James James - 2 months ago 16
Javascript Question

jQuery not equals to conditions not working

I am likely missing something very obvious here but I have 2 values I need to check

if(data['width'] != 2000 || data['width'] != 750){
$('.image-warning').show();
}


I want to show the image warning if the returned value is not 2000 or 750 but seems to be showing all the time.

Answer Source

Your logic is flawed. You can't use a negative check like this with an or condition, because if one side fails the other will hit, hence the condition is always true. You need to use a logical and condition instead:

if (data['width'] != 2000 && data['width'] != 750){
    $('.image-warning').show();
}