Pinkie
Pinkie

Reputation: 10246

jQuery hasClass conditional

The conditional should be met only if the clicked element has no class agallery or aslide. This is not working for me.

if(!$(this).hasClass('agallery') || !$(this).hasClass('aslide')){
  //do certain things
}

Upvotes: 2

Views: 13375

Answers (3)

jfriend00
jfriend00

Reputation: 707248

There is also the non jQuery way using a regex on the class name directly:

if (!this.className.match(/\bagallery\b|\baslide\b/)) {
    // do certain things
}

Upvotes: 2

Matthew Flaschen
Matthew Flaschen

Reputation: 284786

if(!($(this).hasClass('agallery') || $(this).hasClass('aslide'))){

Basically, you should read this as "not (either A or B)", while Clive's equivalent is "(not A) and (not B)".

Your condition "(not A) or (not B)" is true if it doesn't have one. So it will only be false if it has both classes.

Upvotes: 6

Clive
Clive

Reputation: 36957

Try using && instead:

if(!$(this).hasClass('agallery') && !$(this).hasClass('aslide')){
  //do certain things
}

Upvotes: 2

Related Questions