/ Published in: JavaScript

Function to look if an element has a class, if it doesn't simply add it to the element. If it does have a class add it to the class that is already there. Function expects 2 values, the element to add the class to, and the new class name (value).
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; } }
Comments
