CSS Classes
Alright, seems like we've figured out CSS, although wait — we should also cover class manipulations, another one of those essential skills:
add a class to an element
add a class using a callback function
check if an element has a specific class
remove a class
remove a class using a callback function
toggle a class
toggle a class based on the state flag
toggle a class using a callback function, the state flag is optional
I've never once had to use these methods with callback functions, and only once did the "
state" flag come in handy, so don't bother memorizing all of this. Going forward, when quoting the jQuery docs, I'll deliberately skip some of these "features".
But enough translating the official documentation, let's move on to some visual examples.
First up — adding classes:
// add the "active" class
$("#my").addClass("active")
// add multiple classes at once
$("#my").addClass("active notice")Toggling classes is also a popular method:
Here's how toggling multiple classes works:
And removal examples, for the complete set, so to speak:
An interactive example:
Last updated