Try this - it may work for you:
$(function(){
var srch = [ "foo", "bar" ] ;
// make a Regex from the search terms
var attr_regex = '('+srch.join('|')+')';
$('#gallery li').each(function(){
if ($(this).attr("title").match(attr_regex)) {
$(this).css({background:'#f00'});
}
});
});
on 28/08/2009 21:31 tatlar said::
Oops. This sentence:
"I want to traverse the array, looking at the title attribute, and if
it is in the array $search I want the style changed. "
Should be:
"I want to traverse the list, looking at the title attribute, and if
it is in the array $search I want the style changed."
And this:
var $title_to_search = value;
// pseudo code.... I know this is junk
$gallery.find('li').attr('title',$title).css('background-
color','red');
Should be:
var $title_to_search = value;
// pseudo code.... I know this is junk
$gallery.find('li').attr('title',$title_to_search).css('background-
color','red');
On Aug 28, 1:27 pm, tatlar <robertlnew...@gmail.com> wrote:
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.