Javascript Reference - HTML DOM Input Password Object








The Input Password object represents an HTML <input> element with type="password".

Input Password Object Properties

Property Description
autocomplete Sets or gets the autocomplete attribute of a password field
autofocus Sets or gets whether a password field can auto focus when the page loads
defaultValue Sets or gets the default value of a password field
disabled Disable or enable the password field
form Get the form that contains the password field
maxLength Sets or gets the maxlength attribute of a password field
name Sets or gets the name attribute of a password field
pattern Sets or gets the pattern attribute of a password field
placeholder Sets or gets the placeholder attribute of a password field
readOnly Sets or gets whether a password field is read-only, or not
required Sets or gets if the password field must be filled before submitting a form
size Sets or gets the size attribute of a password field
type Returns which type of form element a password field is
value Sets or gets the value attribute of the password field




Input Password Object Methods

Method Description
select() Selects the content of a password field

Standard Properties and Events

The Input Password object supports the standard properties and events.

Example

We can access an <input> element with type="password" by using getElementById().


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

</body>
</html>

The code above is rendered as follows:





Example 2

We can create an <input> element with type="password" by using the document.createElement() method.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!--   w  w w .j a v a2  s  .  com-->
    var x = document.createElement("INPUT");
    x.setAttribute("type", "password");
    x.setAttribute("value", "pswtext");
    document.body.appendChild(x);
}
</script>
</body>
</html>

The code above is rendered as follows: