Javascript Reference - HTML DOM Input Password autofocus Property








The autofocus property sets or gets if a password field can auto focus when the page loads.

Browser Support

autofocus Yes Yes Yes Yes Yes

Syntax

Return the autofocus property.

var v = passwordObject.autofocus 

Set the autofocus property.

passwordObject.autofocus=true|false

Property Values

Value Description
true|false Set if a password field can auto focus when the page loads
  • true - The password field gets focus
  • false - Default. The password field does not get focus




Return Value

A Boolean type value, true if the password field can automatically get focus when the page loads, otherwise false.

Example

The following code shows how to check if a password field can auto focus upon page load.


<!DOCTYPE html>
<html>
<body>
<!--from  w w w .  j av  a 2 s.  co  m-->
Password: <input type="password" id="myPsw" autofocus>
<button onclick="myFunction()">test</button>
<p id="demo"></p>

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

</body>
</html>

The code above is rendered as follows: