Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change document.activeElement in JavaScript?

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

like image 867
mgamer Avatar asked Oct 22 '10 09:10

mgamer


People also ask

What is activeElement?

Definition of 'active element' An active element is an element capable of generating electrical energy. The essential role of this active element is to magnify an input signal to yield a significantly larger output signal.

How do you find the ID of a focused element?

It can be used to get the currently focused element in the document: Syntax: var ele = document. activeElement; Return value: It returns the currently focused element in the document.

What is a focused element?

The focused element is the element that will receive keyboard and similar events by default. By default the browser will scroll the element into view after focusing it, and it may also provide visible indication of the focused element (typically by displaying a "focus ring" around the element).


2 Answers

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(); 
like image 168
Tim Down Avatar answered Oct 05 '22 09:10

Tim Down


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

like image 31
Nick Craver Avatar answered Oct 05 '22 10:10

Nick Craver