Is it possible to change document.activeElement in JavaScript?

JavascriptFocus

Javascript Problem Overview


Is it possible to programmatically change the value of the document.activeElement property in JavaScript?

Javascript Solutions


Solution 1 - Javascript

In IE, use the setActive() method of the element that you want to be the active element. In other browsers that support activeElement, you can use the focus() method of the element, so long as the element is capable of receiving the focus (form elements, editable elements, elements with tabindex set).

If you want to set the activeElement back to the default (the <body> element in most browsers), just call the active element's blur() method:

document.activeElement.blur();

Solution 2 - Javascript

You can just .focus() the element you want and it'll be the new document.activeElement.

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
QuestionmgamerView Question on Stackoverflow
Solution 1 - JavascriptTim DownView Answer on Stackoverflow
Solution 2 - JavascriptNick CraverView Answer on Stackoverflow