How to find elements that match multiple conditions

Dmitri picture Dmitri · Mar 7, 2013 · Viewed 28.9k times · Source

What I need to do is find all a elements on the page that contain "tag" in the href property and the text of the element contains certain word.

For example

<a href="/my/tag/item2">cars</a>

I know I can get all a elements with tag in the href like this:

$("a[href*=tag]");

I can also find all a elements with 'cars' in the text like this:

$("a:contains('cars')");

But how do I combine these 2 conditions into a single statement that says: Find all a elements that contain 'tag' in the href and contain 'cars' in the text?

Answer

iMoses picture iMoses · Mar 7, 2013

Have you tried $("a[href*=tag]:contains('cars')");?

You can go on and on forever that way, i.e.

$("a.className.anotherClass[href*=tag][title=test]:contains('cars'):visible");