Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 + '&nbsp;';         $('#showdata').append('<div class="field">' + i + detail + '</div>');        }  }); 
like image 855
MG1 Avatar asked May 23 '12 15:05

MG1


People also ask

Can IF statement have 2 conditions?

Use two if statements if both if statement conditions could be true at the same time. In this example, both conditions can be true. You can pass and do great at the same time. Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.

How do you write an if statement with multiple conditions?

When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)

Can you put two conditions in an if statement JavaScript?

You can use the logical AND (&&) and logical OR (||) operators to specify multiple conditions in an if statement. When using logical AND (&&), all conditions have to be met for the if block to run.


2 Answers

Try

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

or

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

that says if i does not equals InvKey OR PostDate

like image 73
CD Smith Avatar answered Sep 25 '22 04:09

CD Smith


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'))  
like image 44
SLaks Avatar answered Sep 23 '22 04:09

SLaks