Skip to content

Instantly share code, notes, and snippets.

@jdavidbakr
Created May 20, 2015 14:27
Show Gist options
  • Save jdavidbakr/d50874bd6efe074a0adb to your computer and use it in GitHub Desktop.
Save jdavidbakr/d50874bd6efe074a0adb to your computer and use it in GitHub Desktop.
JavaScript: Class Management
function addClass(el, className) {
if (el.classList)
el.classList.add(className);
else
el.className += ' ' + className;
}
function removeClass(el, className) {
if (el.classList)
el.classList.remove(className);
else
el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment