Javascript DOM HTML Input Checkbox autofocus Property get

Introduction

Find out if a checkbox automatically gets focus upon page load:

var x = document.getElementById("myCheck").autofocus;

Click button to find out if the checkbox automatically gets focus when the page loads.

View in separate window

<!DOCTYPE html>
<html>
<body>

Checkbox: <input type="checkbox" id="myCheck" autofocus>
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {/*from   ww  w . j ava  2  s . c  om*/
  var x = document.getElementById("myCheck").autofocus;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

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

This property mirrors the HTML autofocus attribute.

Property Values

Value Description
true The checkbox gets focus
false Default. The checkbox does not get focus

The autofocus property accepts and returns a boolean value.

It returns true if the checkbox automatically gets focus when the page loads, otherwise it returns false.




PreviousNext

Related