Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select elem with href not starting with some string

I have links like this:

<a href="my:stuff">My Link</a>
<a href="other:stuff">My Link</a>
<a href="test:stuff">My Link</a>
<a href="test:stuff">My Link</a>

I want to select links with href that doesn't start with "test"

I've tried the following but they don't seem to work:

$('a[href!^="test"]')
$('a:not([href^="test"]')

I don't want to use $.filter or anything like that. Is there a way to do this with selectors alone?

like image 690
Vic Avatar asked Aug 08 '26 01:08

Vic


1 Answers

The !^= selector does not exist. However, your second selector should work after including the closing parentheses ')'.

// Incorrect syntax
$('a:not([href^="test"]')

// Correct syntax
$('a:not([href^="test"])')

or you can choose to use the .not() method instead of the :not() pseudo-selector:

$('a').not('[href^="test"]')

Here's a proof-of-concept of the first (using :not()) and second (using .not()) solutions.

like image 76
Terry Avatar answered Aug 10 '26 15:08

Terry