HTML5 classList API
Having thrust myself into the world of JavaScript and JavaScript Libraries, I've often wondered: When are browser vendors going to see the helper methods/libraries created by the JavaScript toolkits and implement these functionalities natively within the browser? I realize that standards are important and browser vendors can't afford to half-ass these implementations but I do believe they could be...expedited. The good news is that one of these functionalities has been add to the HTML5 API; classList.
The classList object, added to all nodes within the DOM, provides developers methods by which to add, remove, and toggle CSS classes on a node. classList also allows developers to check if a CSS class has been assigned to a given node.
node.classList
{
length: {number}, /* # of class on this element */
add: function() { [native code] },
contains: function() { [native code] },
item: function() { [native code] }, /* by index */
remove: function() { [native code] },
toggle: function() { [native code] }
}
node.classList, as you can see, is a small but useful collection of methods.
Adding a CSS Class
myDiv.classList.add('myCssClass');
Removing a CSS Class
myDiv.classList.remove('myCssClass');
Toggling a CSS Class
myDiv.classList.toggle('myCssClass'); //now it's added
myDiv.classList.toggle('myCssClass'); //now it's removed
Note: If toggle is called and the element does not have the provided CSS class, the class is added.
Contains CSS Class Check
myDiv.classList.contains('myCssClass'); //returns true or false
Mozilla Firefox is the only browser that currently supports the classList API. As more browsers add classList support, look for the JavaScript libraries to include classList checks instead of parsing an element's class attribute!
Comments
Be Heard!
Share your thoughts without being a jerk! And wrap your code in <code> tags, f00!
I feel it’s going to be a standard that won’t be implemented for a long time to come across all vendors, but it is certainly worth the wait. And at least we have libraries to fall back on for the moment.
Just wish that there was one JavaScript engine and all used it. Only in a perfect world I guess, and that would also apply for CSS(3) etc.
@Alex Hall: I hope that’s not the case — seems *very* easy to implement.
@Alex Hall:
I wish there was one browser and all used it. That’d be bliss! :)
It’s a step forward, but I prefer Mootools’ Element.addClass over Element.classList.add
@Jani Peltoniemi: I do too, but MooTools could (and probably will, once more browsers implement classList) use these native methods to more quickly add/remove/toggle classes.
very good! Has now come true?
Feature test for HTML5 classList:
if (!!document.createElement('p').classList) {// native classList support
};
mathias, the more ‘accepted’ way to test for such properties is:
'classList' in document.createElement('p')This way also properties that are 0 or false or empty string or null are found. (In this case it doesn’t matter because the classList object never evaluates to false, but still…)
That’s good stuff, but hmm chaining seems not working (FF4):
element.classList.add(“added-class”).remove(“removed-class”);
does not work :/
If there is room for improvement, I wish toggle, remove and add would be available for chaining. What you guys think?
Supported browsers includes Firefox and Google Chrome. Maybe Safari too? I am not sure about that…
Thanks for the great article!