Posted By

1man on 02/19/07


Tagged

class function add


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

kyokutyo
shachi
vali29
chrisjlee


addClass Function


 / 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).

  1. function addClass(element, value) {
  2. if(!element.className) {
  3. element.className = value;
  4. } else {
  5. newClassName = element.className;
  6. newClassName+= " ";
  7. newClassName+= value;
  8. element.className = newClassName;
  9. }
  10. }

Report this snippet  

You need to login to post a comment.