Easy Tutorial
❮ Prop Password Disabled Prop Reset Value ❯

Input Text Object

Input Text Object

Example

Modify the value of a text field:

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

Definition and Usage

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

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


Browser Support

All major browsers support the value property.


Syntax

Return the value property:

Set the value property:

Property 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 a text field:

var x = document.getElementById("myText").value;

x output result:

Mickey

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("The name cannot exceed 10 characters");
    submitOK = "false";
}
if (isNaN(age) || age < 1 || age > 100) {
    alert("The age must be between 1 and 100");
    submitOK = "false";
}
if (at == -1) {
    alert("Invalid e-mail!");
    submitOK = "false";
}
if (submitOK == "false") {
    return false;
}

Example

Dropdown list in a form:

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

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;

Related Pages

HTML Reference: HTML <input> value Attribute


❮ Prop Password Disabled Prop Reset Value ❯