Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM hasFocus() method

HTML DOM hasFocus() method

Element Object Reference Element object

Example

Output text information after the document gains focus:

var

x = document.getElementById(

"demo"

);

if

(document.hasFocus()) {

    x.innerHTML =

"The document has gained focus."

;

}

else

{

    x.innerHTML =

"The document has lost focus."

;

}

Try it »


Definition and usage

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


Browser support

The numbers in the table indicate the first browser version that supports this method.

Method
hasFocus() 30.0 6.0 3.0 Yes 23.0

Syntax

document.hasFocus()

Parameters

None

Technical description

| Return value: | A Boolean value used to detect whether the document or an element has gained focus:

 
  • true - The document has gained focus
  • false - The document has not gained focus
  • | | --- | --- |

    Element Object Reference Element object