The value
property sets or gets the value attribute of a text field.
The value property is supported in all major browsers.
value |
Yes | Yes | Yes | Yes | Yes |
Return the value property.
var v = textObject.value
Set the value property.
textObject.value=text
Value | Description |
---|---|
text | Set the value of the input text field |
A String type value representing the value of the text field.
The following code shows how to get the value of a text field.
<!DOCTYPE html>
<html>
<body>
<!--from ww w .ja v a 2 s. c o m-->
First Name: <input type="text" id="myText" value="Mickey">
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myText").value;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to change the value of a text field.
<!DOCTYPE html>
<html>
<body>
<!--from w w w .java 2 s. c om-->
Name: <input type="text" id="myText" value="Mickey">
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
document.getElementById("myText").value = "from java2s.com";
}
</script>
</body>
</html>
The code above is rendered as follows: