HTML DOM Input Text defaultValue Property


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 −

Updated on: 19-Feb-2021

348 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements