Javascript Reference - HTML DOM Input Password placeholder Property








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

The placeholder attribute sets a short hint text that describes the expected value of a password field.

Browser Support

placeholder Yes 10.0 Yes Yes Yes

Syntax

Return the placeholder property.

var v = passwordObject.placeholder

Set the placeholder property.

passwordObject.placeholder=text




Property Values

Value Description
text Set a short hint text that describes the expected value of the password field

Return Value

A String type value representing a short hint text.

Example

The following code shows how to get the placeholder text of a password field.


<!DOCTYPE html>
<html>
<body>
Password: <input type="password" id="myPsw" placeholder="Your password">
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from  w  ww  . j  a v  a2  s  . c o  m-->
    var x = document.getElementById("myPsw").placeholder;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the placeholder text of a password field.


<!DOCTYPE html>
<html>
<body>
<!--   ww  w . j  a  va2 s  .  co  m-->
Password: <input type="password" id="myPsw" placeholder="Psw..">
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myPsw").placeholder = "Type your password";
}
</script>

</body>
</html>

The code above is rendered as follows: