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.


Following is the syntax for −

Setting the name property − = name

Here, name is for specifying the password field name.


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

Live Demo

<!DOCTYPE html>
<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>
   function changeName() {
      document.getElementById("PASS").name ="NEW_PASS" ;
      document.getElementById("Sample").innerHTML = "Password field name is now NEW_PASS";


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";

Updated on: 19-Feb-2021


Kickstart Your Career

Get certified by completing the course

Get Started