Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a <option> in a <select> menu with CSS?

I've realized that Chrome, it seems, will not allow me to hide <option> in a <select>. Firefox will.

I need to hide the <option>s that match a search criteria. In the Chrome web tools I can see that they are correctly being set to display: none; by my JavaScript, but once then <select> menu is clicked they are shown.

How can I make these <option>s that match my search criteria NOT show when the menu is clicked? Thanks!

like image 534
Jesse Atkinson Avatar asked Feb 10 '12 21:02

Jesse Atkinson


People also ask

How do I hide options in select?

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.

How do you hide a drop down list in CSS?

Answer: Use the CSS :hover pseudo-class If you simply want to show and hide dropdown menu on mouse hover you don't need any JavaScript. You can do this simply using the CSS display property and :hover pseudo-class.

How do you hide the contents of an element in CSS?

You can hide an element in CSS using the CSS properties display: none or visibility: hidden. display: none removes the entire element from the page and mat affect the layout of the page. visibility: hidden hides the element while keeping the space the same.


1 Answers

For HTML5, you can use the 'hidden' attribute.

<option hidden>Hidden option</option> 

It is not supported by IE < 11. But if you need only to hide a few elements, maybe it would be better to just set the hidden attribute in combination with disabled in comparison to adding/removing elements or doing not semantically correct constructions.

<select>      <option>Option1</option>    <option>Option2</option>    <option hidden>Hidden Option</option>  </select>

Reference.

like image 129
Lukas Jelinek Avatar answered Oct 10 '22 11:10

Lukas Jelinek