Option text Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Option

Description

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

Set the text property with the following Values

Value Description
text Sets the text of the option element

Return Value

A String, representing the text of the option element

The following code shows how to change the text of an option element in a drop-down list:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

Select your favorite fruit:
<select>
  <option id="a">Apple</option>
  <option id="orange">Orange</option>
  <option id="pineapple">Pineapple</option>
  <option id="banana">Banana</option>
</select>/*w  ww.  ja  va 2  s. c om*/

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {
    document.getElementById("a").text = "newTextForApple";
}
</script>

</body>
</html>

Related Tutorials