Javascript Reference - HTML DOM Input Text defaultValue Property








The defaultValue property sets or gets the default value of a text field.

Browser Support

defaultValue Yes Yes Yes Yes Yes

Syntax

Return the defaultValue property.

var v = textObject.defaultValue 

Set the defaultValue property.

textObject.defaultValue=value

Property Values

Value Description
value Set the default value of the text field




Return Value

A String type value representing the default value of the text field.

Example

The following code shows how to get the default value of a text field.


<!DOCTYPE html>
<html>
<body>
Name: <input type="text" id="myText" value="abc">
<button type="button" onclick="myFunction()">test</button>
<p id="demo"></p>
<!--from  w  w w .  j  a va 2 s.  c  om-->
<script>
function myFunction() {
    var x = document.getElementById("myText").defaultValue;
    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 default value of a text field.


<!DOCTYPE html>
<html>
<body>
<!--from   w ww  . ja va2  s  . c o m-->
First Name: <input type="text" id="myText" value="abc">
<button type="button" onclick="myFunction()">test</button>

<script>
function myFunction() {
    document.getElementById("myText").defaultValue = "def";
}
</script>

</body>
</html>

The code above is rendered as follows: