5

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.

4
  • 2
    Opposite as in "doesn't start with" or "ends with"?
    – Dennis
    Commented Jun 27, 2013 at 1:35
  • Does opposite mean, does not start with "ma"? Or do you want endswith instead of startswith? Commented Jun 27, 2013 at 1:35
  • not selector, learn about it. Commented Jun 27, 2013 at 1:36
  • Doesn't start with... Commented Jun 27, 2013 at 1:49

3 Answers 3

14

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

$('div:not([class^="ma"])')
4
  • 3
    Or, alternatively, use .not(): $('div').not('[class^="ma"]'). Commented Jun 27, 2013 at 1:36
  • 3
    Except that :not is faster : jsperf.com/not-vs-not32 (tested with chrome, it is maybe a false statement for other broswer) Commented Jun 27, 2013 at 1:39
  • 1
    @Karl-André Gagnon: jsperf.com/not-vs-not32/2 - it's a typo in .not() argument. Doesn't change results though
    – zerkms
    Commented Jun 27, 2013 at 1:45
  • Faster in Firefox too. IE 8 was... inconclusive.
    – Simon C
    Commented Jun 27, 2013 at 1:45
3

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)

3
  • 1
    It's not a selector, but a filtering function. Selector is :not()
    – zerkms
    Commented Jun 27, 2013 at 1:38
  • 3
    @DanielGimenez Some friendly advice: Please do not lecture users on how to answer. More specifically telling them to delete the answer. We indicate usefulness or un-usefulness of posts by downvotes, if you think it's a copy - you can flag it. A lot of users consider a comment like the one you just left rude. Commented Jun 27, 2013 at 1:41
  • 1
    @BenjaminGruenbaum thanks for the advice. The answer has changed from the original, but it was pretty much the same answer just given two minutes after. Seemed cheap. I was trying to give jackmno a chance to delete it before I downvoted in case it was accidental. Commented Jun 27, 2013 at 2:20
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.