/

HTML / CSS / JavaScript Tutorial

HTML Element: <optgroup>

[this page | pdf | back links]

The HTML <optgroup> element indicates a group of related options in a drop-down list.

 

The attributes it can take (other than HTML global attributes and HTML event attributes) include:

 

Attribute

Description

More

disabled

Specified element(s) (here the option-group) to be disabled

Here

label

Title of option group

Here

 

To create or access such an element in JavaScript see here. The corresponding HTML DOM object supports standard DOM properties and methods, and additional properties with the same name and meaning as the attributes of the underlying HTML element referred to above). The default style applicable to this element is shown here.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head></head>
<body>
Created using HTML:<br>
<select size="5">
  <optgroup label="Fruit">
    <option value="fruit1">Apple</option>
    <option value="fruit2">Banana</option>
  </optgroup>
  <option value="veg">Vegetables</option>
</select>

<br><br>Created using JavaScript:<br>
<span id="element"></span>

<script>
var x = document.createElement("SELECT")
var x1 = document.createElement("OPTGROUP");
var x1a = document.createElement("OPTION");
var x1b = document.createElement("OPTION");
var x2 = document.createElement("OPTION");
x1a.textContent = "Apple"; x1a.setAttribute("value","fruit1");
x1.appendChild(x1a);
x1b.textContent = "Banana"; x1b.setAttribute("value","fruit2");
x1.appendChild(x1b);
x1.setAttribute("label","Fruit");
x.appendChild(x1);
x2.textContent = "Vegetable"; x2.setAttribute("value","veg");
x.appendChild(x2);
x.setAttribute("size","5");
document.getElementById("element").appendChild(x);
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | HTML Elements


Desktop view | Switch to Mobile