JamesNZ JamesNZ - 1 year ago 96
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!

Answer Source

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>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download