HTML DOM Input Password autofocus Property


The HTML DOM input Password autofocus property is associated with the HTML <input> element’s autofocus attribute. This property is used for setting or returning if the input password field should be automatically focused when the page loads or not.

Syntax

Following is the syntax to −

Set the autofocus property −

passwordObject.autofocus = true|false

Here, true represents that the password field should get focus and false represents otherwise. It is set to false by default.

Example

Let us look at an example for the Input Password autofocus property −

<!DOCTYPE html>
<html>
<body>
<h1>Input password autofocus property</h1>
Password: <input type="password" id="PASS" autofocus>
<br><br>
<button onclick="FocusVal()">CHECK FOCUS</button>
<p id="Sample"></p>
<script>
   function FocusVal() {
      var P = document.getElementById("PASS").autofocus;
      document.getElementById("Sample").innerHTML = "The password field has autofocus property set to "+P;
   }
</script>
</body>
</html>

Output

This will produce the following output −

On clicking the CHECK FOCUS button −

In the above example −

We have created an input field with type “password”, id “Pass” and it has the autofocus property enabled i.e set to true −

Password: <input type="password" id="PASS" autofocus>

We have then created a button CHECK FOCUS that will execute the FocusVal() method when clicked by the user −

<button onclick="FocusVal()">CHECK FOCUS</button>

The FocusVal() method gets the input element with type password using the getElementById() method and gets its autofocus property. The autofocus property returns true and false depending on the element autofocus attribute value. This value is assigned to variable P and displayed in the paragraph with id “Sample” using its innerHTML property −

function FocusVal() {
   var P = document.getElementById("PASS").autofocus;
   document.getElementById("Sample").innerHTML = "The password field has autofocus property set to "+P;
}

Updated on: 09-Aug-2019

201 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements