
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
HTML DOM hasFocus() method.
The HTML DOM hasFocus() method is used for knowing if the document or any element inside the document has focus. It does so by returning a boolean value in which true represents the document/element has focus and false represents otherwise.
Syntax
Following is the syntax for hasFocus() method −
document.hasFocus()
Example
Let us look at an example for the hasFocus() method −
<!DOCTYPE html> <html> <body> <h1>hasFocus() method</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> <button id="BTN1" >CHECK </button> <script> setInterval("checkFocus()", 20); function checkFocus() { var b = document.getElementById("BTN1"); if (document.hasFocus()) { b.innerHTML = "FOCUSED"; } else { b.innerHTML = "NOT FOCUSED"; } } </script> </body> </html>
Output
This will produce the following output −
On clicking any where in the document, the NOT FOCUSED will change to FOCUSED −
In the above example −
We have created a function checkFocus() that gets the button element using the getElementById() method. It then calls the hasFocus() method to know if the document has focus and execute the conditional statements accordingly. It displays different text on the button if the document.hasFocus() returned true or false −
function checkFocus() { var b = document.getElementById("BTN1"); if (document.hasFocus()) { b.innerHTML = "FOCUSED"; } else { b.innerHTML = "NOT FOCUSED"; } }
Since clicking on the document will always get it focused, we use the setInterval() method that will execute the checkFocus() method every 20 milliseconds to check if the document currently has focus or not −
setInterval("checkFocus()", 20);
- Related Articles
- HTML DOM addEventListener() Method
- HTML DOM normalize( ) Method
- HTML DOM write() Method
- HTML DOM writeln() Method
- HTML DOM insertAdjacentElement( ) Method
- HTML DOM insertAdjacentHTML( ) Method
- HTML DOM insertAdjacentText( ) Method
- HTML DOM insertBefore( ) Method
- HTML DOM isDefaultNamespace( ) Method
- HTML DOM isEqualNode( ) Method
- HTML DOM isSameNode( ) Method
- HTML DOM item( ) Method
- HTML DOM appendChild() Method
- HTML DOM blur() Method
- HTML DOM click() method
