Input Text value attribute
Definition and usage
value
Set or return the value of the text field's value attribute.
The HTML value attribute includes a default value or a value entered by the user (or a value set by the script).
See also:
HTML Reference Manual:HTML <input> value Attribute
Instance
Example 1
Change the value of the text field:
document.getElementById("myText").value = "Bill Gates";
More examples are provided below the page.
Syntax
Return the value attribute:
textObject.value
Set the value attribute:
textObject.value = text
Attribute value
Value | Description |
---|---|
text | Specify the value of the input text field. |
Technical details
Return value: | The string value represents the value of the text field. |
---|
More examples
Example 2
Get the value of a text field:
var x = document.getElementById("myText").value;
Example 3
Form validation:
var at = document.getElementById("email").value.indexOf("@"); var age = document.getElementById("age").value; var fname = document.getElementById("fname").value; submitOK = "true"; if (fname.length > 10) { alert("The name may have no more than 10 characters"); submitOK = "false"; if (isNaN(age) || age < 1 || age > 100) { alert("The age must be a number between 1 and 100"); submitOK = "false"; if (at == -1) { alert("Not a valid e-mail!"); submitOK = "false"; if (submitOK == "false") { return false;
Example 4
Dropdown list in the form:
var mylist = document.getElementById("myList"); document.getElementById("favorite").value = mylist.options[mylist.selectedIndex].text;
Example 5
Another dropdown list:
var no = document.getElementById("no"); var option = no.options[no.selectedIndex].text; var txt = document.getElementById("result").value; txt = txt + option; document.getElementById("result").value = txt;
Example 6
Example showing the difference between the defaultValue and value attributes:
var x = document.getElementById("myText"); var defaultVal = x.defaultValue; var currentVal = x.value;
Browser Support
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support |