Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM Input Text value property

Input Text value property

Input Text Object Reference The Input Text object

Example

Modify the value of the text field:

document.getElementById("myText").value = "Johnny Bravo";

Try it »


Definition and Usage

The value property can set or return the value property value of the text field.

The value property contains the default value or the value entered by the user (or set via a script).


Browser support

Internet Explorer Firefox Opera Google Chrome Safari

All major browsers support the value attribute.


Syntax

Return the value attribute:

textObject

.value

Set the value attribute:

textObject

.value=

text

Attribute values

Value Description
text Tracks the initial value of the text field

Technical details

Return Value: 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;

x The output result is:

Mickey

Try it »

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;

}

Try it »

Example

Dropdown list of the form:

var mylist = document.getElementById("myList");

document.getElementById("favorite").value = mylist.options[mylist.selectedIndex].text;

Try it »

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;

Try it »


Related pages

HTML Reference Manual: HTML value attribute


Input Text Object Reference The Input Text object