HTML DOM Input Password object

HTMLWeb DevelopmentFront End Technology

The HTML DOM Input Password object is associated with the <input> element with type “password”. We can create and access an input element with type password using the createElement() and getElementById() methods respectively.

Properties

Following are the properties for the password object −

Sr.NoProperty & Description
1autocomplete
To set or return the autocomplete attribute value of a password field
2autofocus
To set or return if the password field should automatically get focus when the page loads.
3defaultValue
To set or return the password field default value.
4disabled
To set or return whether the password field is disabled, or not.
5form
To return the reference of the form containing the password field
6maxLength
To set or return the maxlength attribute value of a password field.
7name
To or return the name attribute value of a password field
8pattern
To set or return pattern attribute value of a password field
9placeholder
To set or return the placeholder attribute value of a password field
10readOnly
To set or return if the password field is read-only, or not
11required
To set or return if it is mandatory to fill the password field before submitting the form or not.
12size
To set or return size attribute value of the password field.
13type
It is a read only property returning the type of form element the password field is.
14value
To set or returns the value attribute value of the password field.

Methods

Following is the method for the password object −

Sr.NoMethod & Description
1select()
To select the password field contents.

Syntax

Following is the syntax for −

Creating an input password object −

var P = document.createElement("INPUT");
P.setAttribute("type", "password");

Let us look at an example for the Input Password object −

<!DOCTYPE html>
<html>
<head>
<script>
   function createPASS() {
      var P = document.createElement("INPUT");
      P.setAttribute("type", "password");
      document.body.appendChild(P);
   }
</script>
</head>
<body>
<p>Create an input field with type password by clicking the below button</p>
<button onclick="createPASS()">CREATE</button>
<br><br>
PASSWORD:
</body>
</html>

Output

This will produce the following output −

On clicking the CREATE button and typing in the newly created password field −

In the above example −

We have a button CREATE that will execute the createPass() method on being clicked by the user −

<button onclick="createPASS()">CREATE</button>

The createPass() method uses the createElement() method of the document object to create the <input> element by passing “INPUT” as a parameter. The newly created input element is assigned to variable P and using the setAttribute() method we create a type attribute with value password. Remember, setAttribute() creates the attribute and then assigns its value if the attribute doesn’t exist previously. Finally, using the appendChild() method on document body we append the input element with type password as the child of the body −

function createPASS() {
   var P = document.createElement("INPUT");
   P.setAttribute("type", "password");
   document.body.appendChild(P);
}
raja
Updated on 22-Aug-2019 08:17:52

Advertisements