Javascript DOM HTML Input Button autofocus Property get

Introduction

Find out if a button can automatically get focus on page load:

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

Click button to check if the input button can automatically get focus on page load.

View in separate window

<!DOCTYPE html>
<html>
<body>
<input type="button" id="myBtn" autofocus value="Input Button">
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {/*from   www.  j a va  2  s. c o m*/
  var x = document.getElementById("myBtn").autofocus;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The autofocus property sets or gets whether an input button can automatically get focus when the page loads, or not.

This property mirrors the HTML autofocus attribute.

Property Values

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

The autofocus property return a boolean value.

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




PreviousNext

Related