Javascript Reference - HTML DOM Input Text value Property








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

Browser Support

The value property is supported in all major browsers.

value Yes Yes Yes Yes Yes

Syntax

Return the value property.

var v = textObject.value 

Set the value property.

textObject.value=text

Property Values

Value Description
text Set the value of the input text field




Return Value

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

Example

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:





Example 2

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: