Javascript Reference - HTML DOM Input Password value Property








The value property sets or gets the value attribute of a password field.

Browser Support

value Yes Yes Yes Yes Yes

Syntax

Return the value property.

var v = passwordObject.value 

Set the value property.

passwordObject.value= text

Property Values

Value Description
text Set the value of the password field




Return Value

A String type value representing the password of the password field.

Example

The following code shows how to change the value of the password field.


<!DOCTYPE html>
<html>
<body>
<!--from   www . j  a va  2 s.  com-->
<form>
Password: <input type="password" id="myPsw">
</form>
<button onclick="display()">Display value</button>
<button onclick="change()">Change value</button>
<script>
function display() {
    var x = document.getElementById("myPsw").value;
    console.log("The value is: " + x);
}

function change() {
    var x = document.getElementById("myPsw").value = "NewPassword";
    console.log("The value was changed to: " + x);
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get the value attribute of a password field.


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

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

</body>
</html>

The code above is rendered as follows: