A hidden attribute on an <optgroup> tag hides the optgroup element.
Although the optgroup is not visible, its position on the page is maintained.
A hidden attribute on an <optgroup>element.
The Europe option group is not visible.
<select>
<option value="">-- Select city -- </option>
<optgroup label="Europe" hidden>
<option value="stockholm">Stockholm</option>
<option value="barcelona">Barcelona</option>
<option value="athens">Athens</option>
</optgroup>
<optgroup label="Asia">
<option value="bangkok">Bangkok</option>
<option value="manila">Manila</option>
<option value="jakarta">Jakarta</option>
</optgroup>
</select>
The hidden attribute hides the <optgroup> element.
You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid.
A hidden <optgroup> element is not visible, but it maintains its position on the page.
Removing the hidden attribute makes it re-appear.
<optgroup hidden>
<optgroup hidden="hidden">
Value | Description |
---|---|
hidden | Use 'hidden' or hidden='hidden'. Both are valid. |
Clicking the button toggles A hidden attribute on the Europe <optgroup>.
<select>
<option value="">-- Select city -- </option>
<optgroup id="myoptgroup" label="Europe">
<option value="stockholm">Stockholm</option>
<option value="barcelona">Barcelona</option>
<option value="athens">Athens</option>
</optgroup>
<optgroup label="Asia">
<option value="bangkok">Bangkok</option>
<option value="manila">Manila</option>
<option value="jakarta">Jakarta</option>
</optgroup>
</select>
<br />
<button onclick="toggle(this)">Hide optgroup</button>
<script>
let toggle = button => {
let element = document.getElementById("myoptgroup");
let hidden = element.getAttribute("hidden");
if (hidden) {
element.removeAttribute("hidden");
button.innerText = "Hide optgroup";
} else {
element.setAttribute("hidden", "hidden");
button.innerText = "Show optgroup";
}
}
</script>
Initially, the Europe <optgroup> is visible and its options are selectable.
Clicking the button executes JavaScript that toggles the hidden attribute.
The hidden attribute hides the <optgroup> element.
And the <option> elements inside the optgroup are also hidden.
Here is when hidden support started for each browser:
Chrome
|
1.0 | Sep 2008 |
Firefox
|
1.0 | Sep 2002 |
IE/Edge
|
1.0 | Aug 1995 |
Opera
|
1.0 | Jan 2006 |
Safari
|
1.0 | Jan 2003 |