I ran across an interesting problem today (Firefox 44). I have a conditional statement that is evaluating the else if statement after evaluating a 'true' if statement. Take an example:
The initial state of .dataRow is collapsed. When a dataRow is clicked the first condition is found to be true and the class is changed to expanded. Next the else if statement is evaluated and the class is changed back to collapsed because it changed to expanded in the previous condition! Why in the world would the else if statement be evaluated after the first condition is found true?
$("div").on("click", "div.dataRow", function(){
if ($(this).hasClass("collapsed"))
{
$(this).removeClass("collapsed").addClass("expanded");
}
else if($(this).hasClass("expanded"))
{
$(this).removeClass("expanded").addClass("collapsed");
}
});
I had to put return statements in the conditions to get the function to work properly.
$("div").on("click", "div.dataRow", function(){
if ($(this).hasClass("collapsed"))
{
$(this).removeClass("collapsed").addClass("expanded");
return false;
}
else if($(this).hasClass("expanded"))
{
$(this).removeClass("expanded").addClass("collapsed");
return false;
}
});
EDIT
Here is a simplified reproduction of the problem jsFiddle
elsepart is evaluated if theifstatement evaluated to true. Create a MCVE if you think otherwise.<div><div><div class="dataRow"></div></div></div>