HTML DOM Input Text defaultValue Property

HTMLWeb DevelopmentFront End Technology

The HTML DOM Input Text defaultValue property is used for setting or getting the defaultValue of a text field. The defaultValue of an element is the value assigned to the value attribute. The difference between value property and defaultValue property is the latter retains the original default value specified while the value property value changes based on the user input in the input field.

Syntax

Following is the syntax to set the defaultValue property −

textObject.defaultValue = value

Here, “value” is the text field default value.

Example

Let us look at an example for the Text defaultValue property −

Live Demo

<!DOCTYPE html>
<html>
<body>
<h1>Input Text defaultValue Property</h1>
USERNAME: <input type="text" id="TEXT1" value="RON_1" autofocus>
<p>Change the above text 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("TEXT1").defaultValue="ROHAN_2";
      var P=document.getElementById("TEXT1").defaultValue;
      document.getElementById("Sample").innerHTML = "Default value has been changed from RON_1 to "+P ;
   }
</script>
</body>
</html>

Output

This will produce the following output −

raja
Published on 19-Aug-2019 12:10:43
Advertisements