Javascript DOM HTML Input Text autofocus Property get

Introduction

Find out if a text field automatically gets focus on page load:

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

Click the button to find out if the text field automatically gets focus on page load.

View in separate window

<!DOCTYPE html>
<html>
<body>

Name: <input type="text" id="myText" autofocus>
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {/*  w w  w. ja va2  s . c  om*/
  var x = document.getElementById("myText").autofocus;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

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

This property mirrors the HTML autofocus attribute.

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

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




PreviousNext

Related