Last active
December 24, 2015 13:19
-
-
Save tremby/6804433 to your computer and use it in GitHub Desktop.
Jquery plugins to remove classes from elements by regex and to test if at least one element has a class by regex (same behaviour as .hasClass)
This file contains hidden or 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
$.fn.removeClassRegex = function(regex) { | |
return this.removeClass(function(index, classes) { | |
return classes.split(/\s+/).filter(function(c) { | |
return regex.test(c); | |
}).join(' ') | |
}); | |
}; | |
$.fn.hasClassRegex = function(regex) { | |
var has = false; | |
this.each(function() { | |
var classes = $(this).attr('class'); | |
if (classes == null) { | |
return; | |
} | |
$.each(classes.split(/\s+/), function() { | |
if (regex.test(this)) { | |
has = true; | |
return false; | |
} | |
}); | |
if (has) { | |
return false; | |
} | |
}); | |
return has; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment