Easy Tutorial
❮ Met Canvas Clearrect Jsref Find ❯

Option text Attribute


Definition and Usage

The text attribute sets or returns the text value of an option.

Tip: This attribute sets or returns the plain text contained within the <option> element. This text appears as the label of the option.

Syntax

Set the text attribute:

Return the text attribute:


Browser Support

All major browsers support the text attribute.


Example

The following example returns the text of all options in a dropdown list:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
<script>
function displayResult(selTag){
    var x=selTag.options[selTag.selectedIndex].text;
    alert("You selected: " + x);
}
</script>
</head>
<body>

<form>
Select your favorite fruit:
<select onchange="displayResult(this)">
    <option>Apple</option>
    <option>Orange</option>
    <option>Pineapple</option>
    <option>Banana</option>
</select>
</form>

</body>
</html>

More Examples

Output the text of all options in a dropdown list

❮ Met Canvas Clearrect Jsref Find ❯