Javascript DOM HTML Button autofocus Property get

Introduction

Find out if a button automatically gets focus on page load:

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

Click the button below to find out if the button above automatically gets focus on page load.

View in separate window

<!DOCTYPE html>
<html>
<body>

<button id="myBtn" autofocus>My Button</button>
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {/* www .  ja  v a  2 s. c  om*/
  var x = document.getElementById("myBtn").autofocus;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The autofocus property sets or gets whether a button should automatically get focus on page load.

This property mirrors the HTML autofocus attribute.

The autofocus property accepts and returns a boolean type value.

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

The autofocus property returns true if the button automatically gets focus on page load, otherwise it returns false.




PreviousNext

Related