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: ,

Grab filename from window location

Here is a snippet I use to grab the filename from the full window location of page.

var filename = location.pathname.substr(location.pathname.lastIndexOf("/")+1,
location.pathname.length);

Labels: ,