Javascript Reference - HTML DOM Input Email autofocus Property








The autofocus property sets or gets whether an email field can automatically get focus when the page loads.

Browser Support

autofocus Yes Yes Yes Yes Yes

Syntax

Return the autofocus property.

var v = emailObject.autofocus 

Set the autofocus property.

emailObject.autofocus=true|false

Property Values

Value Description
true|false Specifies whether an email field should get focus when the page loads
  • true - The email field gets focus
  • false - Default. The email field does not get focus




Return Value

A Boolean type value, true if the email field automatically gets focus when the page loads, otherwise false.

Example

The following code shows how to get if an email field automatically can get focus upon page load.


<!DOCTYPE html>
<html>
<body>
<!-- ww  w.j  a  va  2s.co  m-->
E-mail: <input type="email" id="myEmail" autofocus>
<button onclick="myFunction()">test</button>

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

<script>
function myFunction() {
    var x = document.getElementById("myEmail").autofocus;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: