Javascript DOM HTML Option label Property set

Introduction

Change the label value of an option in a drop-down list:

document.getElementById("myOption").label = "newLabel";

Click the button to change the value of the label attribute.

It is not possible to set the label property in Firefox.

View in separate window

<!DOCTYPE html>
<html>
<body>

<select>
  <option label="javascript">Javascript</option>
  <option id="myOption" label="c++">C++</option>
</select>/*  www. j av  a  2 s  .  c om*/
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
  document.getElementById("myOption").label = "newLabel";
  document.getElementById("demo").innerHTML = "The value of the label attribute was changed.";
}
</script>

</body>
</html>

The label property sets or gets the value of the label attribute in an option in a drop-down list.

The label attribute sets a shorter version of an option, which will be displayed in the drop-down list.

Value Description
text Specifies a shorter version for the option

The label property returns a String representing the label of the option in the drop-down list.

If the label attribute is not specified, it will return the <option> element's text content.




PreviousNext

Related