In this section, we will learn what the document hasFocus() method is and how to use it in JavaScript.
What is document hasFocus() Method in JavaScript?
The JavaScript document hasFocus() method is used to check and see if a document or any element inside that document has a focus or not.
For example, if you open a page and click on a text-box element, it will take the focus and hence calling the hasFocus() method will return the value true as a result.
document hasFocus() Method Syntax:
document.hasFocus()
document hasFocus() Method Parameters
The method does not take an argument.
document hasFocus() Method Return Value
The return value of this method is a boolean type and will be true if the document or any of its elements have the focus or false if the document or any of its element didn’t have the focus.
Example: using document hasFocus() Method in JavaScript
See the Pen using document hasFocus() Method in JavaScript by Omid Dehghan (@odchan1) on CodePen.
How does document hasFocus() method work?
The moment we click the button on the page, the element and the document get the focus and so the result of calling the `hasFocus()` method will return true.