Javascript Reference - HTML DOM Input Text autofocus Property








The autofocus property sets or gets whether a text field can automatically get focus when the page loads.

Browser Support

autofocus Yes 10.0 Yes Yes Yes

Syntax

Return the autofocus property.

var v = textObject.autofocus 

Set the autofocus property.

textObject.autofocus=true|false

Property Values

Value Description
true|false Specifies whether a text field can get focus when the page loads
  • true - The text field gets focus
  • false - Default. The text field does not get focus




Return Value

A Boolean type value, true if the text field can get focus when the page loads, otherwise false.

Example

The following code shows how to check if a text field is auto focused.


<!DOCTYPE html>
<html>
<body>
<!--from  ww  w .  ja va  2s. com-->
Name: <input type="text" id="myText" autofocus>
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myText").autofocus;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: