I am working with jquery and attempting to add a class to a table on the selection of that table row.
I was initially using the following code -
$(this)
addClass
does just that, adds the class to the element.
toggleClass
on the other hand does THAT, toggles the class, removing it if it's there, otherwise adding it, but optionally taking a boolean value (true/false) to determine if the object should be added (true) or removed (false).
toggleClass
probably wasn't working for you in the instances where this.clicked
was false
, which is expected behavior. The argument you're passing in addClass
has no effect, since it ALWAYS adds the class.
Conclusion:
Use toggleClass
for toggling classes, use addClass
for adding classes.