JamesNZ JamesNZ - 7 months ago 29
jQuery Question

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"


Links to a PDF file


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


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


You just combine them as normal:


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>