THE WORLD'S LARGEST WEB DEVELOPER SITE

Option text Property

❮ Option Object

Example

Change the text of an option element in a drop-down list:

document.getElementById("apple").text = "newTextForApple";
Try it Yourself »

Definition and Usage

The text property sets or returns the text of an option element.

Tip: If the value property is not specified for an option element, then the text content will be sent to the server when the container form is submitted.


Browser Support

Property
text Yes Yes Yes Yes Yes

Syntax

Return the text property:

optionObject.text

Set the text property:

optionObject.text = text

Property Values

Value Description
text Specifies the text of the option element


Technical Details

Return Value: A String, representing the text of the option element

More Examples

Example

Return the text of the selected option in a drop-down list:

var x = selTag.options[selTag.selectedIndex].text;
Try it Yourself »

Example

Get the text of all options in a drop-down list:

var x = document.getElementById("mySelect");
var txt = "All options: ";
var i;
for (i = 0; i < x.length; i++) {
    txt = txt + "\n" + x.options[i].text;
}
Try it Yourself »

❮ Option Object