Input Text valueProperty
Example
Modify the value of the text field:
document.getElementById("myText").value = "Johnny Bravo";
Try it Yourself »
Definition and Usage
The value property sets or returns the value of the value attribute of the text field.
The value property contains the default value or the value entered by the user (or set by script).
Browser Support
![]()
All major browsers support the value property.
Syntax
Return the value property:
textObject.value
Set the value property:
textObject.value=text
Property Value
| Value | Description |
|---|---|
| text | Specifies the initial value of the text field |
Technical Details
| Return value: | A string, representing the default value of the text field |
|---|
More Examples
Example
Get the value of the text field:
var x = document.getElementById("myText").value;
xThe output result is:
Mickey
Try it Yourself »
Example
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("Name cannot exceed 10 characters");
submitOK = "false";
}
if (isNaN(age)||age<1||age>100) {
alert("Age range is 1 to 100");
submitOK = "false";
}
if (at==-1) {
alert("Invalid e-mail!");
submitOK = "false";
}
if (submitOK=="false") {
return false;
}
var age = document.getElementById("age").value;
var fname = document.getElementById("fname").value;
submitOK = "true";
if (fname.length>10) {
alert("Name cannot exceed 10 characters");
submitOK = "false";
}
if (isNaN(age)||age<1||age>100) {
alert("Age range is 1 to 100");
submitOK = "false";
}
if (at==-1) {
alert("Invalid e-mail!");
submitOK = "false";
}
if (submitOK=="false") {
return false;
}
Try it Yourself »
Example
Form dropdown list:
var mylist = document.getElementById("myList");
document.getElementById("favorite").value = mylist.options[mylist.selectedIndex].text;
document.getElementById("favorite").value = mylist.options[mylist.selectedIndex].text;
Try it Yourself »
Example
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;
var option = no.options[no.selectedIndex].text;
var txt = document.getElementById("result").value;
txt = txt + option;
document.getElementById("result").value = txt;
Try it Yourself »
Related Pages
HTML Reference Manual:HTML <input> value attribute
Input Text Object Other Extensions