Is it possible to programmatically change value of document.activeElement property in JavaScript? T
+1
A:
You can just .focus()
the element you want and it'll be the new document.activeElement
.
Nick Craver
2010-10-22 09:34:07
+2
A:
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();
Tim Down
2010-10-22 09:36:50
Let's take the page at jquery.com as an example. When you enter the page and check document.activeElement in FireBug you get "<body class="jq-enhanced">". Then let's say that you change activeElement do some link element (<a>). How can you after that reset activeElement to the previous one (body). I can't do it using document.body.focus();
mgamer
2010-10-22 09:45:02
Use `document.activeElement.blur();`. Answer updated.
Tim Down
2010-10-22 09:55:16
@Tim Down That is perfect. Thx!
mgamer
2010-10-22 09:56:30