Dofactory.com
Dofactory.com

HTML <option> hidden Attribute

A hidden attribute on an <option> tag hides the option.

Although the option is not visible, its position on the page is maintained.

Example

#

A hidden attribute on an <option> tag.
The Los Angeles option is not visible.

<select>
  <option>-- Select city -- </option>
  <option hidden>Los Angeles</option>
  <option>New York</option>
  <option>Houston</option>
  <option>Chicago</option>
</select>

Using hidden

The hidden attribute hides the <option> element.

You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid.

A hidden <option> element is not visible, but it maintains its position on the page.

Removing the hidden attribute makes it re-appear.


Syntax

<option hidden>
or
<option hidden="hidden">

Values

#

Value Description
hidden Use 'hidden' or hidden='hidden'. Both are valid.

More Examples

Clicking the button toggles A hidden attribute on the Los Angeles <option> element.


<select>
  <option>-- Select city -- </option>
  <option id="myoption">Los Angeles</option>
  <option>New York</option>
  <option>Houston</option>
  <option>Chicago</option>
</select>

<br />
<button onclick="toggle(this)">Hide option</button>

<script>
  let toggle = button => {
    let element = document.getElementById("myoption");
    let hidden = element.getAttribute("hidden");
    
    if (hidden) {
       element.removeAttribute("hidden");
       button.innerText = "Hide option";
    } else {
       element.setAttribute("hidden", "hidden");
       button.innerText = "Show option";
    }
  }
</script>

Code explanation

Initially, all <option> elements are visible.

Clicking the button calls JavaScript which toggles A hidden attribute on the Los Angeles option.

With the hidden attribute applied this option is invisible.


Browser support

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

You may also like

 Back to <option>

Author: Jack Poorte
Published: Jun 20 2021
Last Reviewed: Sep 30 2023


What's your favorite/least favorite part of Dofactory?


Guides