user191688
user191688

Reputation: 2659

jQuery select element containing a link that contains any element of array

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

Upvotes: 2

Views: 1055

Answers (1)

Boris Gu&#233;ry
Boris Gu&#233;ry

Reputation: 47585

$('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();
}

Upvotes: 2

Related Questions