Javascript Reference - HTML DOM Input Button autofocus Property








The autofocus property sets or gets whether an input button can automatically get focus when the page loads, or not.

Browser Support

autofocus Yes 10.0 Yes Yes Yes

Syntax

Return the autofocus property.

var v = buttonObject.autofocus 

Set the autofocus property.

buttonObject.autofocus=true|false




Property Values

Value Description
true|false Specifies whether an input button should get focus when the page loads
  • true - The input button gets focus
  • false - Default. The input button does not get focus

Return Value

A Boolean, returns true if the input button automatically gets focus when the page loads, otherwise it returns false.

Example

The following code shows how to check if a button can automatically get focus when page loads.


<!DOCTYPE html>
<html>
<body>
<!--from w w  w  .  j  a  va 2s. co m-->
<input type="button" id="myBtn" autofocus value="Input Button">
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("myBtn").autofocus;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: