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

HTML DOM focus() method

HTML DOM focus() method

Element Object Reference Element object

Example

is Set focus on the element:

document.getElementById(

"myAnchor"

).focus();

Try it


Definition and usage

The focus() method is used to set focus on an element (if it can be set).

**Tip:** Use blur() method to remove focus from the element.


Browser support

Method
focus() Yes Yes Yes Yes Yes

Syntax

HTMLElementObject

.focus()

Parameters

None

Technical description

Return Value: No return value

More examples

Example

Set focus on the text field:

document.getElementById(

"myText"

).focus();

Try it »

Example

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

window.onload =

function

() {

    document.getElementById(

"myText"

).focus();

};

Try it »


Related pages

HTML DOM Reference Manual: onfocus event


Element Object Reference Element object