tomhoppe.com

Racing, Web Development, Photography, and Beer...Stuff that matters.

Monday, February 11, 2008

Add class name to div id

Here is a script to add an extra class name to a div with an id regardless of what is there now.

function addClassName (id, classname) {
   var e;
   if (typeof(id) == 'string') {e = this.getElementById(id);} 
   else {e = id;}

   if (!e || typeof e.className != 'string') {
      throw "Cannot add class to element " + id;
   }

   /* Check if the class is already there */
   if (!e.className.match(new RegExp('\\b' + classname + '\\b'))) {
      e.className += ' ' + classname;
   }
}

Labels: ,

0 Comments:

Post a Comment

<< Home