HTML DOM Element focus() method

Definition and usage

focus() Methods that allow an element to gain focus (if it is focusable).

See also:

blur() method (remove focus)

onfocus Event

Example

Example 1

Focus on an element:

document.getElementById("myAnchor").focus();

Try it yourself

Example 2

Place the focus on the text field:

document.getElementById("myText").focus();

Try it yourself

Example 3

After the document is loaded, place the focus on the text field:

window.onload = function() {
  document.getElementById("myText").focus();
}

Try it yourself

Syntax

element.focus()

Parameter

None.

Return value

None.

Browser support

element.focus() It is a DOM Level 2 (2001) feature.

All browsers fully support it:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support