addClass doesn’t seem to work under certain circumstances for me. Have a look at the following code:
$("#my-div-container .my-sub-div li a").live('mouseover', function(e){
e.preventDefault();
var li_parent_elem = $(this).parent();
li_parent_elem.siblings().removeClass();
switch(true)
{
case (li_parent_elem.is("#my-div-container .my-sub-div li:first-child") == true):
//alert("First");
li_parent_elem.addClass("first");
break;
case (li_parent_elem.is("#my-div-container .my-sub-div li:last-child") == true):
//alert("Last");
li_parent_elem.addClass("last");
break;
default:
//alert("Mid");
li_parent_elem.addClass("mid");
}
});
addClass doesn’t work for the first two case statements, only the default. However, when I uncomment the alert directives, I get the correct alert so I know the conditional statement is working.
I’m not sure what is wrong here, and I’d appreciate some assistance.
After a bit of fiddling I seem to have found a solution to this, even though I don’t really like it because I don’t understand why it works and the earlier code doesn’t.
Here’s the modified code:
I’m still at a loss why even when I removed the switch statement and used if-else with my earlier code, it didn’t work, but this does.
Maybe it’s a bug or something.