Javascript DOM HTML Option text Property set

Introduction

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

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

Click the button to change the text for the apple option in the dropdown list.

View in separate window

<!DOCTYPE html>
<html>
<body>

Select your favorite:
<select>
  <option id="apple">Apple</option>
  <option id="orange">Orange</option>
  <option id="pineapple">Pineapple</option>
  <option id="banana">Banana</option>
</select>//from   w  w w . jav a  2  s.c o m
<button onclick="myFunction()">Test</button>

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

</body>
</html>

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

If the value property is not set for an option element, the text content will be sent to the server.

The text property accepts and returns a String type value.




PreviousNext

Related