MG1
MG1

Reputation: 1687

jQuery multiple conditions within if statement

What is the syntax for this loop to skip over certain keys? The way I have it written is not working properly.

 $.each(element, function(i, element_detail){
    if (!(i == 'InvKey' && i == 'PostDate')) {
        var detail = element_detail + ' ';
        $('#showdata').append('<div class="field">' + i + detail + '</div>');
       }
 });

Upvotes: 30

Views: 248462

Answers (3)

WTellos
WTellos

Reputation: 19

A more general approach:

if ( ($("body").hasClass("homepage") || $("body").hasClass("contact")) && (theLanguage == 'en-gb') )   {

       // Do something

}

Upvotes: 0

CD Smith
CD Smith

Reputation: 6607

Try

if (!(i == 'InvKey' || i == 'PostDate')) {

or

if (i != 'InvKey' || i != 'PostDate') {

that says if i does not equals InvKey OR PostDate

Upvotes: 60

SLaks
SLaks

Reputation: 887453

i == 'InvKey' && i == 'PostDate' will never be true, since i can never equal two different things at once.

You're probably trying to write

if (i !== 'InvKey' && i !== 'PostDate')) 

Upvotes: 14

Related Questions