
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
HTML DOM Input Password defaultValue Property
The HTML DOM Input Password defaultValue property is used for setting or getting the defaultValue of a password field. The defaultValue of an element is the value assigned to the value attribute. The difference between value property and defaultValue property is that the defaultValue property retains the original default value specified while the value property change based on the user input in the input field.
Syntax
Following is the syntax to set the defaultValue property −
passwordObject.defaultValue = value
Here, “value” is the password field default value.
Example
Let us look at an example for the Input Password defaultValue property −
<!DOCTYPE html> <html> <body> <h1>Input Password defaultValue Property</h1> Password: <input type="password" id="PASS" value="abcd123"> <p>Change the above password field default value by clicking on the CHANGE button</p> <button type="button" onclick="changeDefault()">CHANGE</button> <p id="Sample"></p> <script> function changeDefault() { document.getElementById("PASS").defaultValue="Password1234"; var P=document.getElementById("PASS").defaultValue; document.getElementById("Sample").innerHTML = "Default value has been changed from abc123 to "+P ; } </script> </body> </html>
Output
This will produce the following output −
On clicking the CHANGE button −
In the above example −
We have first created a password input field with id “PASS” and value=”abcd123”.
Password: <input type="password" id="PASS" value="abcd123">
We have then created a button CHANGE that will execute the changeDefault() method on being clicked by the user −
<button type="button" onclick="changeDefault()">CHANGE</button>
The changeDefault() method uses the getElementById() method to get the input field with type password and sets it defaultValue property to “Password123”. We then get the defaultValue property of the input with type password by again using the getElementById() method and assigning it to variable P. This variable is then displayed in the paragraph with id “Sample” using the innerHTML property of the paragraph −
function changeDefault() { document.getElementById("PASS").defaultValue="Password1234"; var P=document.getElementById("PASS").defaultValue; document.getElementById("demo").innerHTML = "Default value has been changed from abc123 to "+P ; }
- Related Articles
- HTML DOM Input Month defaultValue Property
- HTML DOM Input Number defaultValue Property
- HTML DOM Input Color defaultValue Property
- HTML DOM Input Email defaultValue Property
- HTML DOM Input Time defaultValue Property
- HTML DOM Input URL defaultValue Property
- HTML DOM Input Week defaultValue Property
- HTML DOM Input Search defaultValue Property
- HTML DOM Input Text defaultValue Property
- HTML DOM Input Password value property
- HTML DOM Input Password pattern property
- HTML DOM Input Password placeholder property
- HTML DOM Input Password readOnly property
- HTML DOM Input Password required property
- HTML DOM Input Password size property
