I have been told that jquery functions can be chained to each other.
So I was wondering if this code:
if (cDiv.hasClass('step-dark-left'))
cDiv.removeClass('step-dark-left').addClass('step-light-left');
Could be changed by removing the if like so:
cDiv.removeClass('step-dark-left').addClass('step-light-left');
So, if .removeClass fails, then .addClass wont execute?
removeClass() will not fail if the element doesn’t expose the
step-dark-leftclass. It will just do nothing and return the jQuery object, therefore addClass() will always be called.So, you’ll have to keep your
ifstatement around, unless you can work the class check in the jQuery selector itself. For instance, if your<div>element has itsidattribute set tocDiv:That way, the jQuery object will be empty if the element doesn’t expose the
step-dark-leftclass to begin with, so bothremoveClass()andaddClass()will end up doing nothing in that case.