How do you clear the focus in javascript?

JavascriptFocus

Javascript Problem Overview


I know this shouldn't be that hard, but I couldn't find the answer on Google.

I want to execute a piece of javascript that will clear the focus from whatever element it is on without knowing ahead of time which element the focus is on. It has to work on firefox 2 as well as more modern browsers.

Is there a good way to do this?

Javascript Solutions


Solution 1 - Javascript

Answer: document.activeElement

To do what you want, use document.activeElement.blur()

If you need to support Firefox 2, you can also use this:

function onElementFocused(e)
{
    if (e && e.target)
        document.activeElement = e.target == document ? null : e.target;
} 

if (document.addEventListener) 
    document.addEventListener("focus", onElementFocused, true);

Solution 2 - Javascript

.focus() and then .blur() something else arbitrary on your page. Since only one element can have the focus, it is transferred to that element and then removed.

Solution 3 - Javascript

document.activeElement.blur();

Works wrong on IE9 - it blurs the whole browser window if active element is document body. Better to check for this case:

if (document.activeElement != document.body) document.activeElement.blur();

Solution 4 - Javascript

None of the answers provided here are completely correct when using TypeScript, as you may not know the kind of element that is selected.

This would therefore be preferred:

if (document.activeElement instanceof HTMLElement)
    document.activeElement.blur();

I would furthermore discourage using the solution provided in the accepted answer, as the resulting blurring is not part of the official spec, and could break at any moment.

Solution 5 - Javascript

For anyone using typescript, use

(document.activeElement as HTMLElement).blur();

This makes sure the element is of type HTMLElement

Solution 6 - Javascript

dummyElem.focus() where dummyElem is a hidden object (e.g. has negative zIndex)?

Solution 7 - Javascript

You can call window.focus();

but moving or losing the focus is bound to interfere with anyone using the tab key to get around the page.

you could listen for keycode 13, and forego the effect if the tab key is pressed.

Solution 8 - Javascript

With jQuery its just: $(this).blur();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndresView Question on Stackoverflow
Solution 1 - JavascriptjpsView Answer on Stackoverflow
Solution 2 - JavascriptKevin ReidView Answer on Stackoverflow
Solution 3 - Javascriptpwnzor1337View Answer on Stackoverflow
Solution 4 - JavascriptWilco BrouwerView Answer on Stackoverflow
Solution 5 - Javascriptm_j_alkarkhiView Answer on Stackoverflow
Solution 6 - JavascriptplodderView Answer on Stackoverflow
Solution 7 - JavascriptkennebecView Answer on Stackoverflow
Solution 8 - JavascriptbsbakView Answer on Stackoverflow