HTML DOM Input Password name Property


The HTML DOM Input Password name property is used for setting or returning the name attribute of an input password field. The name attribute helps in identifying the form data after it has been submitted to the server. JavaScript can also use the name attribute to refer form elements to manipulate later on.

Syntax

Following is the syntax for −

Setting the name property −

passwordObject.name = name

Here, name is for specifying the password field name.

Example

Let us look at an example for the password name property −

<!DOCTYPE html>
<html>
<body>
<h1>Input Password name Property</h1>
Password: <input type="password" id="PASS" name="passW">
<p>Change the name of the password field by clicking the below button</p>
<button onclick="changeName()">CHANGE NAME</button>
<p id="Sample"></p>
<script>
   function changeName() {
      document.getElementById("PASS").name ="NEW_PASS" ;
      document.getElementById("Sample").innerHTML = "Password field name is now NEW_PASS";
   }
</script>
</body>
</html>

Output

This will produce the following output −

On clicking the CHANGE NAME button −

In the above example −

We have first created an input password field with id “PASS” and having its name attribute value set to “passW” −

Password: <input type="password" id="PASS" maxLength=”5”>

We have then created a button CHANGE NAME that will execute the changeName() when clicked by the user −

<button type="button" onclick="changeName()">CHANGE NAME</button>

The changeName() method uses the getElementById() method to get the input field with type password and sets it name property to “NEW_PASS”. We then show this change by displaying a message in the paragraph with id “Sample” using its innerHTML property −

function changeName() {
   document.getElementById("PASS").name ="NEW_PASS" ;
   document.getElementById("Sample").innerHTML = "Password field name is now NEW_PASS";
}
raja
Published on 09-Aug-2019 11:02:16
Advertisements