THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML

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 »

Related Pages

HTML DOM reference: onfocus event


HTMLElement Object Reference Element Object