Is it possible to change document.activeElement in JavaScript?

mgamer picture mgamer · Oct 22, 2010 · Viewed 64.4k times · Source

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

Answer

Tim Down picture Tim Down · Oct 22, 2010

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();