Remove a class name from an element
Login or Register to Bookmark this snippet
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).
function removeClassName(e,t) {
if (typeof e == "string") {
e = xGetElementById(e);
}
//code to change and replace strings
var ec = ' ' + e.className.replace(/^s*|s*$/g,'') + ' ';
var nc = ec;
t = t.replace(/^s*|s*$/g,'');
//check if not already there
if (ec.indexOf(' '+t+' ') != -1) {
//found, so lets remove it
nc = ec.replace(' ' + t.replace(/^s*|s*$/g,'') + ' ',' ');
}
//return the changed text!
e.className = nc.replace(/^s*|s*$/g,''); //trimmed whitespace
return true;
}
Added by JC on 12th November, 2007
There are no comments about this snippet.
You must be registered and logged in to post a comment.
Login here to post a comment