Deckard
Deckard

Reputation: 1433

CSS selector that ends with something and not contains something

I need to find any <a> tags, which its href ends with 'pdf' and not contains 'test'.

like this;

<a href="/abc/tes.pdf">link</a>

All I know is the first condition;

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

Teach me the rest.

Thank you.

Upvotes: 1

Views: 1680

Answers (1)

Ernest
Ernest

Reputation: 8829

jQuery('a[href!="test"][href$=".pdf"]');

Or better:

jQuery('a[href$=".pdf"]').not('[href="test"]');

And here's the reason:

Because [name!="value"] is a jQuery extension and not part of the CSS specification, queries using [name!="value"] cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use $("your-pure-css-selector").not('[name="value"]') instead.

EDIT: The "better" may not be correct here, [value$="name"] isn't CSS2 pure selector either, but jQuery documentation doesn't mention that. Some additional research is required here...

EDIT 2: It looks like I didn't thought this through. What you probably want is:

jQuery('a[href$=".pdf"]').not('a[href*="test"]');

Which reads: select all a elements which have href attribute that ends with ".pdf" but do not select a elements which have "test" as a substring anywhere in href attribute.

http://api.jquery.com/category/selectors/

Upvotes: 4

Related Questions