Javascript DOM HTML OptionGroup Object get

Introduction

The OptionGroup object represents an HTML <optgroup> element.

We can access an <optgroup> element via document.getElementById():

var x = document.getElementById("myOptgroup");

Click the button to get the label/description of the option group.

View in separate window

<!DOCTYPE html>
<html>
<body>
<select size="4">
  <optgroup id="myOptgroup" label="Programming Language">
    <option value="javascript">Javascript</option>
    <option value="html">HTML</option>
    <option value=""sql">SQL</option>
  </optgroup>
</select>//from w w  w  . ja va2  s  . co  m
<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>



PreviousNext

Related