JamesNZ
JamesNZ

Reputation: 512

Multiple jQuery selector criteria on a single attribute?

Is it possible to combine multiple jquery selectors on a single attribute? e.g. finding all links to PDF files that are in the "media/pdfs" directory.

Links with an href containing "media/pdfs"

$("a[href*='media/pdfs']")

Links to a PDF file

$("a[href$='.pdf']")

I'm hoping it's possible to combine somehow like below?

$("a[href*='media/pdfs/*.pdf']")

It'd be easily fixed with a regex but the documentation on selectors doesn't say anything about that being possible. Thanks!

Upvotes: 1

Views: 30

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074355

You just combine them as normal:

$("a[href*='media/pdfs'][href$='.pdf']")

Live Example:

$("a[href*='media/pdfs'][href$='.pdf']").css("color", "green");
<div><a href="media/pdfs/foo.pdf">Match</a></div>
<div><a href="media/pdfs/foo.jpg">Not a match</a></div>
<div><a href="foo.pdf">Not a match</a></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 3

Related Questions