Search code examples
jqueryjquery-selectorsdom-traversal

How to find elements that match multiple conditions


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?


Solution

  • 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");