Abdul
Abdul

Reputation: 1476

Is this possible in coding

In jQuery i want to compare multiple values in if() parenthesis, my requirement is as below. Currently I am trying this in jQuery, let me know if it works in other environment.

if((a==b && c==d) || (a!=b && c!=d)){
// code goes here

} 

Edit

This is my scenario;

 if (($(".product_attribute_input" + id + "_Color").children(":first").val() != "" &&  $(".product_attribute_input" + id + "_Size").children(":first").val() != "")  || ($(".product_attribute_input" + id + "_Color").children(":first").val() !== undefined &&  $(".product_attribute_input" + id + "_Size").children(":first").val() !== undefined ))

Upvotes: 0

Views: 81

Answers (2)

JF-Mechs
JF-Mechs

Reputation: 1081

If I understand your scenario correctly, you are trying to check if the product color and size input have value..

//Declare variable here for ease of maintenance and to avoid repetitive calls on the same node...
var productAttrInputColor = $(".product_attribute_input" + id + "_Color").children(":first").val(),
    productAttrInputSize = $(".product_attribute_input" + id + "_Size").children(":first").val();

if (productAttrInputColor || productAttrInputSize) {
    //Do something if the above condition returns a truthy values
}

Also, notice that I removed the conditional operator for checking empty string, undefined. Since the above conditional statement will translate the value into Boolean true, and thus execute the code block.

Upvotes: 0

Adder
Adder

Reputation: 5868

You want an inverted xor operation. This can be shortened to:

if((a==b) == (c==d)){
// code goes here

} 

Upvotes: 3

Related Questions