I need to use the OR operator in a jQuery if statement to filter out 10 states. My code works wile only excluding one state, but fails when I try to include multiple states. Is there a correct way to do this?
Code I am using :
if ((state != 10) || (state != 15) || (state != 19) ||
(state != 22) || (state != 33) || (state != 39) ||
(state != 47) || (state != 48) || (state != 49) ||
(state != 51))
return true;
In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery. A (selector) to "query (or find)" HTML elements.
var flagu6=0; if( flagu1==0 && flagu2==0 && flagu3==0 && flagu4==0 && flagu6==0 ) return true; else return false; } function clearBox(type) { // Your implementation here } // Event handler $submitButton. on('click', handleSubmit); }); Now, clicking the button will go to handleSubmit function, which employs your code.
The :contains() selector selects elements containing the specified string. The string can be contained directly in the element as text, or in a child element. This is mostly used together with another selector to select the elements containing the text in a group (like in the example above).
In this article, we will learn to use ternary or conditional operator in jQuery. The Ternary or Conditional operator takes three operands, a condition followed by question mark followed by two expressions to execute with a semicolon (:) in between the two expressions.
Think about what
if ((state != 10) || (state != 15) || (state != 19) || (state != 22) || (state != 33) || (state != 39) || (state != 47) || (state != 48) || (state != 49) || (state != 51))
means. ||
means "or." The negation of this is (by DeMorgan's Laws):
state == 10 && state == 15 && state == 19...
In other words, the only way that this could be false is if a state
equals 10, 15, and 19 (and the rest of the numbers in your or statement) at the same time, which is impossible.
Thus, this statement will always be true. State 15 will never equal state 10, for example, so it's always true that state
will either not equal 10 or not equal 15.
Change ||
to &&
.
Also, in most languages, the following:
if (x) {
return true;
}
else {
return false;
}
is not necessary. In this case, the method returns true
exactly when x
is true and false
exactly when x
is false
. You can just do:
return x;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With