views:

159

answers:

1

Hi,

I am trying to work out a complicated jQuery selection. I have an array, and I need to select div.foo that contains a link a.bar and the link's href contains any of the strings in the array.

For example:

<html>
<div class=foo>
  some text
  <a class=bar href=example.com?q=widget&id=23456789>search</a>
</div>
</html>

and jScript

widgets[0]='12345678';
widgets[1]='23456789';
widgets[2]='34567890';

I want to highlight this div because the query string contains 23456789 which is one of the elements in widgets[].

Thanks

+2  A: 
$('div.foo > a.bar[href*=23456789');

select all a which contains '23456789' in the href attributes with the bar class which are direct descendant of all div with a class foo.

for (num in widgets)
{
    $('div.foo > a.bar[href*=' + num + ']').doSomething();
}
Boris Guéry
for (*num* in widgets) ... :)
mamoo
@mamoo, héhé thank you ;)
Boris Guéry
@Boris, .dosomething() applies to a.bar. I needed to add .parent() to apply to div.foo, but this is what I was looking for.Thanks!