/ Published in: JavaScript
Description: A function that add a class to an element if the element doesn't have anyone, if it already has one, add a new class name.
Arguments: The elemento to which you want to add the class and the name of the class you want to add.
From book Dom Scripting by Jeremy Keith.
Arguments: The elemento to which you want to add the class and the name of the class you want to add.
From book Dom Scripting by Jeremy Keith.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function addClass(element,value) { if (!element.className) { element.className = value; } else { newClassName = element.className; newClassName+= " "; newClassName+= value; element.className = newClassName; } }