[jQuery] search list in a div for attribute value
i think this is a pretty simple question, but i can't seem to figure
it out.
i have a list in a div:
<div id="gallery">
<ul>
<li title="foo">foo</li>
<li title="bar">bar</li>
<li title="unicorn">unicorn</li>
</ul>
</div>
I have an array with values in it:
var $search = [ "foo", "bar" ] ;
I want to traverse the array, looking at the title attribute, and if
it is in the array $search I want the style changed.
Note that #gallery has about 450 list items - the one above is
abridged for this post.
The array $search will at most have 5 items.
So, is it faster to do a $.each() on the list items, or do a $.each()
on the $search items. I am pretty sure it is faster to do the latter,
but I am not sure how to get the title attribute from the list items
for the match.
Something like?
$.each( $search, function(i, value) {
var $title_to_search = value;
// pseudo code.... I know this is junk
$gallery.find('li').attr('title',$title).css('background-
color','red');
});
Can someone refine this for me? Thanks in advance.