Return to Snippet

Revision: 10982
at January 20, 2009 14:28 by jambalaya


Initial Code
function cssRegex(c) {
	return new RegExp('(?:\\s|^)' + c + '(?:\\s|$)');
}

function hasClass(e,c) {
	return e.className.match(cssRegex(c));
}
 
function addClass(e,c) {
	if (!hasClass(e,c)) {
		e.className += ' '+c;
		return true;
	}
	return false;
}
 
function removeClass(e,c) {
	if (hasClass(e,c)) {
		e.className=e.className.replace(cssRegex(c),' ');
		return true;
	}
	return false;
}

Initial URL

                                

Initial Description

                                

Initial Title
testing, adding, and removing classes from an html element

Initial Tags
css, class

Initial Language
JavaScript