'Trouble using the OR operator in a jQuery if statement
My aim is to check a given ingredient for different attributes with an list of OR statements, but it returns an unexpected || error.
$(".ingredient.clicked").each(function() {
if (typeof $(this).attr("noAddedSugar") != "undefined")
|| (typeof $(this).attr("vegan") != "undefined")
|| (typeof $(this).attr("glutenfree") != "undefined")
{
$('#proba').text("Proba");
} else {
$('#proba').text("");
return false;
}
});
It works when I add and modify the variables individually, does not when I use OR. Any input would be appreciated.
Thanks!
Solution 1:[1]
Your if logic needs to be fully enclosed in a separate set of parentheses:
if ((typeof $(this).attr("noAddedSugar") != "undefined") || (typeof $(this).attr("vegan") != "undefined") || (typeof $(this).attr("glutenfree") != "undefined"))
The way it is now, it ends after the first !="undefined")
The inner parentheses around each condition are not necessary, so you can simplify it a bit more with:
if( typeof $(this).attr("noAddedSugar") != "undefined" || typeof $(this).attr("vegan") != "undefined" || typeof $(this).attr("glutenfree") != "undefined" )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chris |
