X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR BOOTSTRAP XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

HTML DOM focus() Method

HTMLElement Object Reference Element Object

Example

Give focus to an <a> element:

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

Try it yourself »

More "Try it Yourself" examples below.


Definition and Usage

The focus() method is used to give focus to an element (if it can be focused).

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


Browser Support

Method
focus() Yes Yes Yes Yes Yes


Syntax

HTMLElementObject.focus()


Parameters

None.

Technical Details

Return Value: No return value


Examples

More Examples

Example

Give focus to a text field:

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

Try it yourself »

Example

Give focus to a text field, immediately after the document window has been loaded:

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

Try it yourself »


HTMLElement Object Reference Element Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook