Created
April 27, 2017 22:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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