Skip to content

Instantly share code, notes, and snippets.

@5iDS
Created April 28, 2017 08:10
Show Gist options
  • Save 5iDS/963924381fdbdbc43d363aff6e96a724 to your computer and use it in GitHub Desktop.
Save 5iDS/963924381fdbdbc43d363aff6e96a724 to your computer and use it in GitHub Desktop.
Vanilla JS function to remove class from DOM elements
function removeClass(elements, myClass) {
// if there are no elements, we're done
if (!elements) { return; }
// if we have a selector, get the chosen elements
if (typeof(elements) === 'string') {
elements = document.querySelectorAll(elements);
}
// if we have a single DOM element, make it an array to simplify behavior
else if (elements.tagName) { elements=[elements]; }
// create pattern to find class name
var reg = new RegExp('(^| )'+myClass+'($| )','g');
// remove class from all chosen elements
for (var i=0; i<elements.length; i++) {
elements[i].className = elements[i].className.replace(reg,' ');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment