Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
63
rated 0 times [  67] [ 4]  / answers: 1 / hits: 166616  / 14 Years ago, thu, december 9, 2010, 12:00:00

This should work:



$('option').hide(); // hide options


It works in Firefox, but not Chrome (and probably not in IE, not tested).



A more interesting example:



<select>
<option class=hide>Hide me</option>
<option>visible option</option>
</select>
<script type=text/javascript>
// try to hide the first option
$('option.hide').hide();

// to select the first visible option
$('option:visible').first().attr('selected', 'selected');
</script>


Or see the example at http://jsfiddle.net/TGxUf/



Is the only option to detach the option elements from the DOM? I need to show them again later, so this would not be very effective.


More From » jquery

 Answers
30

Unfortunately, you can't hide option elements in all browsers.



In the past when I have needed to do this, I have set their disabled attribute, like so...



$('option').prop('disabled', true);


I've then used the hiding where it is supported in browsers using this piece of CSS...



select option[disabled] {
display: none;
}

[#94662] Tuesday, December 7, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
joannam

Total Points: 331
Total Questions: 106
Total Answers: 105

Location: Sweden
Member since Mon, May 8, 2023
1 Year ago
;