Is it possible to focus on a
using JavaScript focus() function?

Is it possible to focus on a

using JavaScript focus() function?

Is it possible to focus on a

using JavaScript focus() function?
I have a

tag

You have 3 tries left

I am trying to focus on the above

using :
document.getElementById(‘tries’).focus();

But it doesn’t work. Could someone suggest something….?

Solutions/Answers:

Solution 1:

window.location.hash = '#tries';

This will scroll to the element in question, essentially “focus”ing it.

Solution 2:

Yes – this is possible. In order to do it, you need to assign a tabindex…

<div tabindex="0">Hello World</div>

A tabindex of 0 will put the tag “in the natural tab order of the page”. A higher number will give it a specific order of priority, where 1 will be the first, 2 second and so on.

You can also give a tabindex of -1, which will make the div only focus-able by script, not the user.

document.getElementById('test').onclick = function () {
    document.getElementById('scripted').focus();
};
div:focus {
    background-color: Aqua;
}
<div>Element X (not focusable)</div>
<div tabindex="0">Element Y (user or script focusable)</div>
<div tabindex="-1" id="scripted">Element Z (script-only focusable)</div>
<div id="test">Set Focus To Element Z</div>

Obviously, it is a shame to have an element you can focus by script that you can’t focus by other input method (especially if a user is keyboard only or similarly constrained). There are also a whole bunch of standard elements that are focusable by default and have semantic information baked in to assist users. Use this knowledge wisely.

Solution 3:

document.getElementById('tries').scrollIntoView() works. This works better than window.location.hash when you have fixed positioning.

Solution 4:

You can use tabindex

<div tabindex="-1"  id="tries"></div>

The tabindex value can allow for some interesting behaviour.

  • If given a value of “-1”, the element can’t be tabbed to but focus
    can be given to the element programmatically (using element.focus()).
  • If given a value of 0, the element can be focused via the keyboard
    and falls into the tabbing flow of the document. Values greater than
    0 create a priority level with 1 being the most important.

Solution 5:

<div id="inner" tabindex="0">
    this div can now have focus and receive keyboard events
</div>

Solution 6:

I wanted to suggest something like Michael Shimmin’s but without hardcoding things like the element, or the CSS that is applied to it.

I’m only using jQuery for add/remove class, if you don’t want to use jquery, you just need a replacement for add/removeClass

–Javascript

function highlight(el, durationMs) { 
  el = $(el);
  el.addClass('highlighted');
  setTimeout(function() {
    el.removeClass('highlighted')
  }, durationMs || 1000);
}

highlight(document.getElementById('tries'));

–CSS

#tries {
    border: 1px solid gray;
}

#tries.highlighted {
    border: 3px solid red;
}

Related:  What is the difference between JSON and Object Literal Notation?