Javascript Reference - HTML DOM Input Checkbox autofocus Property








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

Browser Support

autofocus Yes 10.0 Yes Yes Yes

Syntax

Return the autofocus property.

var v = checkboxObject.autofocus 

Set the autofocus property.

checkboxObject.autofocus=true|false

Property Values

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




Return Value

A Boolean type value, true if the checkbox can automatically get focus when the page loads, otherwise it returns false.

Example

The following code shows how to get if a checkbox automatically can get focus upon page load.


<!DOCTYPE html>
<html>
<body>
Checkbox: <input type="checkbox" id="myCheck" autofocus>
<p id="demo"></p>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!--from ww w.  ja  v  a2  s .com-->
    var x = document.getElementById("myCheck").autofocus;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: