Option text attribute

Definition and Usage

text Set or return the text of the option element property.

Tip:If no value attribute is specified for the option element, the text content will be sent to the server when the container form is submitted.

See also:

HTML Reference Manual:HTML <option> Tag

Example

Example 1

Change the text of the option element in the dropdown list:

document.getElementById("apple").text = "newTextForApple";

Try It Yourself

Example 2

Return the text of the selected option in the dropdown list:

var x = selTag.options[selTag.selectedIndex].text;

Try It Yourself

Example 3

Get the text of all options in the dropdown 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

Syntax

Return text attribute:

optionObject.text

Set text attribute:

optionObject.text = text

Attribute Value

Value Description
text Specifies the text of the option element.

Technical Details

Return Value: A string value representing the text of the option element.

Browser Support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support