Javascript Reference - HTML DOM Input Submit autofocus Property








The autofocus property sets or gets whether a submit button can auto focus when the page loads.

Browser Support

autofocus Yes 10.0 Yes Yes Yes

Syntax

Return the autofocus property.

var v = submitObject.autofocus 

Set the autofocus property.

submitObject.autofocus=true|false

Property Values

Value Description
true|false Set if a submit button can auto focus when the page loads
  • true - The submit button gets focus
  • false - Default. The submit button does not get focus




Return Value

A Boolean type value, true if the submit button automatically gets focus when the page loads, otherwise false.

Example

The following code shows how to get if a Submit button automatically gets focus upon page load.


<!DOCTYPE html>
<html>
<body>
<!--   ww  w  .ja v a2  s . c  om-->
<input type="submit" id="mySubmit" value="Submit" autofocus>
<button onclick="myFunction()">test</button>

<p id="demo"></p>

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

</body>
</html>

The code above is rendered as follows: