Created
January 13, 2014 04:51
-
-
Save wolever/8394924 to your computer and use it in GitHub Desktop.
jQuery plugin to toggle an element's classes based on a prefix.
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
/** | |
* Toggles the CSS classes of an element based on a prefix:: | |
* > elems = $(".status") | |
* > elems | |
* [<div class="status status-active">, <div class="status status-pending">] | |
* > elems.toggleClassPrefix("status-", "status-pending") | |
* > elems | |
* [<div class="status status-pending">, <div class="status status-pending">] | |
*/ | |
$.fn.toggleClassPrefix = function(prefix, toAdd) { | |
$(this).each(function() { | |
var newClass = $.grep(this.classList, function(cls) { | |
return cls.indexOf(prefix) != 0; | |
}); | |
newClass.push(toAdd); | |
this.className = newClass.join(" "); | |
}); | |
return this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment