Easy Tutorial
❮ Jsref Issafeinteger Number Prop Style Textshadow ❯

HTML DOM hasFocus() Method

Element Object

Example

Output text information after the document gains focus:

var x = document.getElementById("demo");
if (document.hasFocus()) {
    x.innerHTML = "The document has focus.";
} else {
    x.innerHTML = "The document has lost focus.";
}

Definition and Usage

The hasFocus() method returns a boolean value, used to detect whether the document (or any element within the document) has focus.


Browser Support

The numbers in the table specify the first browser version that fully supports the method.

Method Chrome Edge Firefox Safari Opera
hasFocus() 30.0 6.0 3.0 Yes 23.0

Syntax

Parameters

| None |

Technical Description

| Return Value: | Boolean, used to detect whether the document or element has focus: <br> <br> true - The document has focus <br> false - The document does not have focus | | --- | --- |


Element Object

❮ Jsref Issafeinteger Number Prop Style Textshadow ❯