For example if I want to do something if parent element for used element hasn't got ul tag as next element, how can I achieve this?

I try some combination of .not() and/or .is() with no success.

What's the best method for negate code of a if else block?

My Code

if ($(this).parent().next().is('ul')){ // code... } 

I want to achieve this

Pseudo Code:

if ($(this).parent().next().is NOT ('ul')) { //Do this.. } 
4

2 Answers

You can use the Logical NOT ! operator:

if (!$(this).parent().next().is('ul')){ 

Or equivalently (see comments below):

if (! ($(this).parent().next().is('ul'))){ 

For more information, see the Logical Operators section of the MDN docs.

0

Try negation operator ! before $(this):

if (!$(this).parent().next().is('ul')){ 
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy