Aldi Unanto
Aldi Unanto

Reputation: 3682

jQuery opposite of "starts with" selector: [^=]

I have some div tag below:

<div class="magazine"></div>
<div class="newsletter"></div> // I need to take this div
<div class="may-moon"></div>

If I needed div with class start with "ma", I would use $('div[class^="ma"]'), but what is opposite? thanks.

Upvotes: 5

Views: 1677

Answers (3)

Ishan Jain
Ishan Jain

Reputation: 8171

You need to use :not() Selector for this. because there is no exact opposite selector exist of [^=] and * in jquery.

:not() Selector - Selects all elements that do not match the given selector.

See more about Jquery selectors

There a opposite selector exist ! -

Attribute Not Equal Selector [name!="value"] - Select elements that either don’t have the specified attribute, or do have the specified attribute but not with a certain value.

but use of this ! selector, you need to provide full-name of class.

$('div[class!="magazine"][class!="may-moon"]')  

Try This

Upvotes: 0

jacmkno
jacmkno

Reputation: 1317

You can use the negative filtering function "not" like this: $('div').not('[class^="ma"]'), or the negative selector ":not" like this: $('div:not([class^="ma"])') (as pointed by Karl-André Gagnon)

Upvotes: 3

Karl-Andr&#233; Gagnon
Karl-Andr&#233; Gagnon

Reputation: 33870

The opposite would be to use jQuery :not():

$('div:not([class^="ma"])')

Upvotes: 14

Related Questions