Javascript Reference - HTML DOM Button autofocus Property








The autofocus property, new for the <button> element in HTML5, sets or gets whether a button should automatically get focus.

Browser Support

autofocus Yes 10 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 whether to auto focus a button. Default to false.




Return Value

A Boolean value, true if the button automatically gets focus, otherwise false.

Example

The following code shows how to find out if a button can automatically get focus.


<!DOCTYPE html>
<html>
<body>
<button id="myBtn" autofocus>My Button</button>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--   ww w. j  a  va2s . c o m-->
    var x = document.getElementById("myBtn").autofocus;
    document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>

The code above is rendered as follows: