I'm having a hard time trying to understand why this is not working as expected.

I simply am trying to target the body element, but only if does not contain the .logged-in class, but with the code below I get a log to the console whether the class exists or not.

if ($('body:not(.logged-in)')) {
    console.log('Hi!');
}

...or...

if ($('body').not('.logged-in')) {
    console.log('Hi!');
}

Am I missing something here?

1 Answers

1
Ele On Best Solutions

Those selectors actually skip (remove from the whole selected elements) the elements with class logged-in.

I recommend you to use the attribute length in order to check the existence of an element.

if ($('body').find('.logged-in').length) { 
    console.log('Exists!');
}