Javascript Reference - HTML DOM OptionGroup label Property








The label attribute specifies a label/description for an option-group.

The label property sets or gets the label attribute of an option-group.

Browser Support

label Yes Yes Yes Yes Yes

Syntax

Return the label property.

var v = optiongroupObject.label 

Set the label property.

optiongroupObject.label=text

Property Values

Value Description
text Set a description for the option-group




Return Value

A String type value representing the label of the option-group.

Example

The following code shows how to get the description of an option-group.


<!DOCTYPE html>
<html>
<body>
<!--  ww w. j  a  v a  2 s.com-->
<select size="6">
  <option value="A">A</option>
  <option value="B">B</option>
  <optgroup id="myOptgroup" label="Option Group">
    <option value="C">C</option>
    <option value="E">E</option>
  </optgroup>
</select>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("myOptgroup").label;
    document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the description of an option-group.


<!DOCTYPE html>
<html>
<body>
<!--from www . ja va2  s  . c o  m-->
<select size="6">
  <option value="A">A</option>
  <option value="B">B</option>
    <optgroup id="myOptgroup" label="Option Group">
    <option value="C">C</option>
    <option value="E">E</option>
  </optgroup>
</select>
<button onclick="myFunction()">test</button>

<script>
function myFunction() {
    document.getElementById("myOptgroup").label = "new value";
}
</script>

</body>
</html>

The code above is rendered as follows: