347

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

I have a <div> tag

<div id="tries">You have 3 tries left</div>

I am trying to focus on the above <div> using :

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

But it doesn't work. Could someone suggest something....?

6
  • 4
    What do you want to happen when you 'focus' it? Divs don't accept input, so do you want to flash the border, or flash a background highlight etc? Commented Sep 7, 2010 at 7:14
  • @Michael, yes I need that <div> to fetch the attention of user... Commented Sep 7, 2010 at 7:17
  • 1
    possible duplicate of Which HTML elements can receive focus? Commented Jun 12, 2014 at 19:16
  • 1
    @MichaelShimmins and anyone else, <div> elements can accept input if you have contenteditable set to true. ( Reason why I inquired ) Commented Sep 20, 2017 at 3:49
  • 3
    @MichaelShimmins divs can accept input if they overflow and show a scroll bar. When a div with a scroll bar is focused, the arrow keys will scroll its content (instead of the content of other elements such as body). Commented Jul 23, 2018 at 21:37

8 Answers 8

649

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.

Sign up to request clarification or add additional context in comments.

19 Comments

@Michael Shimmins, This allows the element to be focusable (in a non-standard way!) with focus().
@Olical - I'm pretty sure that's exactly what it says in the last line of may answer. Let me know if you think there is a problem that needs an edit.
If it wasn't clear, adding tabindex to an element will make it focusable, which enables the focus() and blur() methods, and then you can trigger a focus like this: document.getElementById('tries').focus();
Hi @SteveOwen - it seems to still work in Firefox v42. I have added a snippet to this answer so you can run it and test.
@SteveOwen using window.location.hash = ... is the way to do that. focus doens't mean "bring into view", it just means literally place the focus on that element.
|
115
window.location.hash = '#tries';

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

5 Comments

@Casey Chu : Its working fine in ie but not in firefox, do you have any idea ?
It doesn't work in some Chrome versions... However, @vinoths solution does it
This works but... with Angular you would have problems!!
@CarlosVerdes; Is there a solution for Angular?
not replying to the 'focus' question, this shouldn't be the correct answer. in my case, I want to focus on an 'contentEditable' div, and your trick doesn't help.
98

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

4 Comments

Note that this solution will not work in IE, Opera or Safari.
I just tried this in Chrome 65 and it does not work. The overlay div scrolls into view, but the focus is still in the background div. The only sure-fire way I know of is to have a tabbable element (using tabindex attribute) in the overlay div and use focus() on that element instead.
@om-the-eternity already stated that he wants the div to call the attention of the user, so what he needs is not to actually focus (even when working it's not correct to do it) the div, but bringing it to the screen, this solves that
If I do this in Chrome it keeps scrolling back to the still-focused input field. Especially if the page isn't done loading yet.
89

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.

2 Comments

Life saver, thanks. In Edge a div can be focused but not in Chrome without this trick. Accepted answer is great but I don't want additional part in the url.
This is def the best answer if you need to do some kinda JS Dom Manipulation for UI. Its really handy for allowing onblur to be used to dismiss elements such as menus or pop up
21
<div id="inner" tabindex="0">
    this div can now have focus and receive keyboard events
</div>

1 Comment

This works for me in Chromium/Chrome 46 only combined with $('#inner').focus();
13

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>

1 Comment

Nice answer, but would be good to also add some explanation to your code example.
3

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;
}

Comments

1

To make the border flash you can do this:

function focusTries() {
    document.getElementById('tries').style.border = 'solid 1px #ff0000;'
    setTimeout ( clearBorder(), 1000 );
}

function clearBorder() {
    document.getElementById('tries').style.border = '';
}

This will make the border solid red for 1 second then remove it again.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.