Skip to content
Advertisement

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!

Advertisement

Answer

   if (typeof $(this).attr("noAddedSugar") != "undefined") || (typeof $(this).attr("vegan") != "undefined") || (typeof $(this).attr("glutenfree") != "undefined") 

should be

if( (typeof $(this).attr("noAddedSugar") != "undefined") || (typeof $(this).attr("vegan") != "undefined") || (typeof $(this).attr("glutenfree") != "undefined")  )
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement