Javascript DOM HTML Input Email autofocus Property get

Introduction

Find out if an email field automatically gets focus on page load:

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

Click the button to find out if the email field can gets focus on page load.

View in separate window

<!DOCTYPE html>
<html>
<body>

E-mail: <input type="email" id="myEmail" autofocus>
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {/*from   w w w .  jav a2 s  .  c o  m*/
  var x = document.getElementById("myEmail").autofocus;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The autofocus property sets or gets whether an email field can automatically get focus on page load.

This property mirrors the HTML autofocus attribute.

The autofocus property accepts and returns a boolean value.

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

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




PreviousNext

Related