The defaultValue
property sets or gets the default value of a text field.
defaultValue |
Yes | Yes | Yes | Yes | Yes |
Return the defaultValue property.
var v = textObject.defaultValue
Set the defaultValue property.
textObject.defaultValue=value
Value | Description |
---|---|
value | Set the default value of the text field |
A String type value representing the default value of the text field.
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:
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: