Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine which html page element has focus? [duplicate]

Possible Duplicate:
How do I find out which DOM element has the focus?

Is there a way in javascript to determine which html page element has focus?

like image 736
Peanut Avatar asked Jan 27 '09 15:01

Peanut


People also ask

How do I find out which DOM element has the focus?

Syntax: var ele = document. activeElement; Return value: It returns the currently focused element in the document.

How do you know which element is focused?

To detect if the element has the focus in JavaScript, you can use the read-only property activeElement of the document object. const elem = document. activeElement; The activeElement returns the currently focused element in the document.

How do you know if an element is focused in HTML?

In HTML document, the document. hasfocus() the method is used for indicating whether an element or document has the focus or not. The function returns a true value if the element is focused otherwise false is returned. This method can be used to determine whether the active element is currently in focus.

Which HTML elements can receive focus?

There isn't a definite list, it's up to the browser. The only standard we have is DOM Level 2 HTML, according to which the only elements that have a focus() method are HTMLInputElement , HTMLSelectElement , HTMLTextAreaElement and HTMLAnchorElement .


1 Answers

Use the document.activeElement property.

The document.activeElement property is supported on Chrome 2+, Firefox 3+, IE4+, Opera 9.6+ and Safari 4+.

Note that this property will only contain elements that accept keystrokes (such as form elements).

like image 103
Aron Rotteveel Avatar answered Sep 29 '22 22:09

Aron Rotteveel