Let's suppose I have a <select>
element:
<select id="foobar" name="foobar" multiple="multiple">
<option value="1">Foobar 1</option>
<option value="2">Foobar 2</option>
<option value="3">Foobar 3</option>
</select>
And let's suppose I have an array of values, something like:
var optionValues = [2, 3];
How can I select the <option>
s with values 2 and 3 most efficiently?
I'm working with a <select>
that has thousands of <option>
s, so doing it manually like this won't work:
var optionElements = [];
$("#foobar").children().each(function() {
if($.inArray($(this).val(), optionValues)) {
optionElements.push($(this));
}
}
It's just too slow. Is there a way to hand jQuery a list of values for the elements I need to select? Any ideas?
P.S. In case you're wondering, I am in the middle of optimizing my jQuery PickList widget which currently sucks at handling large lists.