HTML DOM hasFocus() Method (original) (raw)
Last Updated : 12 Jun, 2023
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.
Syntax:
document.hasfocus();
Parameters: This method has no parameters.
Return Value: The hasfocus() method returns a Boolean value indicating whether an element or document has focus or not.
The below examples illustrate the HTML DOM hasfocus() method:
Example 1: This example illustrates whether the document has a focus or not.
HTML `
HTML DOM hasFocus() MethodClick anywhere in the document to test hasfocus() function.
`
Output:

Explanation: The setinterval() function calls the hasfocustest() in 1 millisecond which after evaluation produces a result.
Example 2: This example illustrates changes background-colour of the heading based on whether the document has focus or not.
HTML `
HTML DOM hasFocus() MethodClick anywhere in the document to test hasfocus() function.