For an upcoming Microformats related project, Drew McLellan asked me to come up with a small script that includes all the tasks you need to deal with classes.
Well, here goes:
mfsct = {
check:function(oElm, strClassName){},
add:function(oElm, strClassName){},
remove:function(oElm, strClassName){},
display:function(o){},
getElements:function(oElm, strTagName, strClassName){}
}
oElm is the element you send to each of the methods, strClassName is the CSS class name to use and strTagName is a tag name to cut down on iterations when looking for elements with classes.check() method tests element has the class applied to it and returns true or false.add() method adds the class to the elementremove() method removes the class from the elemento is an element, the display() method shows its class names, otherwise it displays whatever o is. If your browser has a console (like Firefox with Firebug installed or Safari) the output will show in the console, otherwise it is an alert().getElements() method returns an array of elements with the class applied to them. If you want to scan the whole document, use document as the oElm paramter and * as the strTagName parameter. This is slightly adapted from Robert Nyman's getElementsByClassName, who changed the original version by Jonathan Snook.It is neither new or clever, but maybe useful for you, too.
Download the Microformats scanner class tool mfsct.js
That's the lot. Drop feedback on the blog