Remove a class name from an element

Description Javascript

This function removes a class name from an element and keeping the existing classes.

e is the element id as string or object reference.

t is the class as string.

Requires xGetElementById (available on this site).

The Code Download

  1. function removeClassName(e,t) {
  2.     if (typeof e == "string") {
  3.         e = xGetElementById(e);
  4.     }
  5.     //code to change and replace strings
  6.     var ec = ' ' + e.className.replace(/^s*|s*$/g,'') + ' ';
  7.     var nc = ec;
  8.     t = t.replace(/^s*|s*$/g,'');
  9.     //check if not already there
  10.     if (ec.indexOf(' '+t+' ') != -1) {
  11.         //found, so lets remove it
  12.         nc = ec.replace(' ' + t.replace(/^s*|s*$/g,'') + ' ',' ');
  13.     }
  14.     //return the changed text!
  15.     e.className = nc.replace(/^s*|s*$/g,''); //trimmed whitespace
  16.     return true;
  17. }

Credits

Added by JC on 13th November 2007

Comments

There are no comments about this snippet.

Post Comment Github Markdown Supported

You must be logged in to post a comment.

Login here to post a comment