Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Last active March 4, 2016 08:46
Show Gist options
  • Save andreasvirkus/917a47fd8276be57300e to your computer and use it in GitHub Desktop.
Save andreasvirkus/917a47fd8276be57300e to your computer and use it in GitHub Desktop.
/**
* author: andreas johan virkus
* snippet url: https://gist.github.com/andreasvirkus/917a47fd8276be57300e
*
* Return all classes found that match the given prefix
* Example: You have an element with classes "apple juiceSmall juiceBig banana"
* You run:
* $elem.returnClassPrefix('juiceS');
* The resulting class is "juiceSmall"
*
* @return {Array} List consists of a concatenated string of classes that match the prefix per element iterated
*/
$.fn.returnClassPrefix = function (prefix) {
var matchedClasses = [];
this.each( function ( i, it ) {
var classes = it.className.split(' ').map(function (item) {
return item.indexOf(prefix) === 0 ? item : '';
});
matchedClasses.push(classes.join(' ').trim());
});
return matchedClasses;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment