Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide options in a select list using jQuery

I have an object with key/value pairs of options I want to hide/remove from a select list. Neither of the following option selectors work. What am I missing?

$.each(results['hide'], function(name, title) {                        $("#edit-field-service-sub-cat-value option[value=title]").hide();   $("#edit-field-service-sub-cat-value option[@value=title]").hide(); });  
like image 520
hitfactory Avatar asked Aug 13 '09 11:08

hitfactory


People also ask

How do I hide an option in dropdown?

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 can I show and hide elements based on selected option with jQuery?

If you want to hide/show div on dropdown selected, use the jQuery hide() and show(). Before you perform hide or show div on dropdown selection, you need to hide them first using CSS display:none.

How do I remove dynamically selected options in jQuery?

$(this). find('[value="X"]'). remove();


2 Answers

For what it's worth, the second form (with the @) doesn't exist in jQuery 1.3. The first isn't working because you're apparently expecting variable interpolation. Try this:

$("#edit-field-service-sub-cat-value option[value=" + title + "]").hide(); 

Note that this will probably break in various hideous ways if title contains jQuery selector metacharacters.

like image 173
chaos Avatar answered Sep 28 '22 14:09

chaos


You cannot do this x-browser. If I recall ie has issues. The easiest thing to do is keep a cloned copy of the select before you remove items, this allows you to easily remove and then append the missing items back.

like image 43
redsquare Avatar answered Sep 28 '22 14:09

redsquare