Skip to content

Instantly share code, notes, and snippets.

@chrismauck
Created April 27, 2017 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismauck/b6b6f1288e7faed6c7052c6fb4b37265 to your computer and use it in GitHub Desktop.
Save chrismauck/b6b6f1288e7faed6c7052c6fb4b37265 to your computer and use it in GitHub Desktop.
Modern browser version of https://github.com/chrismauck/cls-js; Helper functions for class manipulation
/*
* cls - helper functions for class manipulation
* https://github.com/chrismauck/cls-js
*
* cls.hasClass( elem, 'class-name' ) => boolean check
* cls.addClass( elem, 'class-name' ) => add class(es)
* cls.removeClass( elem, 'class-name' ) => remove class(es)
* cls.toggleClass( elem, 'class-name' ) => toggle class(es)
*/
(function(window) {
'use strict';
cls = {
hasClass: function(e, c) { return e.classList.contains(c); },
addClass: function(e, c) { e.classList.add(c); },
removeClass: function(e, c) { (c) ? e.classList.remove(c) : e.className = ''; },
toggleClass: function(e, c) { var _this = e, names = c.split(/[ ,]+/).forEach(function(n){ _this.classList.toggle(n); }); }
};
(typeof define === 'function' && define.amd) ? define(cls) : window.cls = cls;
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment