A couple of weeks ago, a client of ours needed a simple way to select some or all options in a multiple select form element that contained hundreds of options. Basically, they had various State/County combination’s that they needed to assign to different pieces of content.
So Article X might need to select all Counties in State Y. In other cases, an article might simply need to be tagged will all available options in a particular select element. The point is, holding the Command key and clicking through hundreds of select options was becoming a bit tedious. They asked if there was a an easier way. jQuery to the rescue! So here are the tasks that I needed jQuery to help with:
- Select all elements with a single click
- Deselect all elements with a single click
- Select elements that match some value. (contain “red”) with a single click
After spending some time digging through the documentation on jQuery.com – I realized all I needed to accomplish these tasks was to attach/alter the selected attribute of any or all option elements. I could attach click functions to radio inputs that would carry out each individual task. To select all elements I could use the “.each” function to loop through the option elements and attach the “selected=’selected’” along the way. For selecting only some elements, (contains “red”) I could use the “:contains” selector.
Here is what I came up with:
$("input#all").click(function(){
$("#colors").each(function(){
$("#colors option").attr("selected","selected");
});
});
$("input#red").click(function(){
// code for selecting all elements with id=colors that contain the value red, rinse and repeat as needed
$("#colors option:contains('Red')").attr("selected","selected");
// additionally disable the element so the user can't muck with it
$("#colors").attr("disabled","disabled");
});
It worked wonderfully! There was just one problem: with each click that was supposed to only select certain elements it was simply adding to what was already selected. So you might end up with Red and Green elements selected – this was not the desired result. I realized that when selecting elements that matched some value, I would first need to deselect anything that may already be selected in that particular element. This was easily accomplished like so:
$("input#red").click(function(){
// loop through each option in the selected element, remove the selected attribute
$("#colors").each(function(){
$("#colors option").removeAttr("selected");
});
// code for selecting all elements with id=colors that contain the value red, rinse and repeat as needed
$("#colors option:contains('Red')").attr("selected","selected");
// additionally disable the element so the user can't muck with it
$("#colors").attr("disabled","disabled");
});
After adding another function for deselecting all options, the work was done and better yet, our client was ecstatic. We hope other developers out there find this tip useful. Demo and source code found below:


Nice post, and a solid tip… how about combination filters, ie. all option 2’s, red + blue, etc… alss, not sure, but is a multiselect list with hundreds of items actually the best UI choice for your use case? how about cosnidering a multi-column list that performs filtering, or perhaps a dropdown controlled set of lists, etc. just a thought.
Thanks Chad! You can certainly use combination filters. In your jQuery selector statement, simply separate each with a comma, like so:
$("#colors option:contains(\'Red\'), #colors option:contains(\'Green\')").attr("selected","selected");As for using multiple selects, you’re right! Probably not the best UI choice. We were simply working within an existing data architecture – definitely something that could be worked on in the future.
What timing! I was just working on selecting multiple select options yesterday!
I figured it out but finding this post beforehand would’ve been great. Thanks for the tip!
How are you guys?
Hey Tim. We’re doing great, and hope that you are, too. Glad that the tutorial ended up being of some use to you, albeit being a day late.
This solution relies on the fact that the group of options contains the same value like “red”. In case you don’t have that available, the way of doing this would be to add a do-nothing class to the group of options, like the example as below:
A
B
C
D
E
F
G
then to select all options except B and D, we write:
$(”#letter options:has(.needSelect)”).attr(’selected’, ’selected’)