Skip to content

Instantly share code, notes, and snippets.

@podgorniy
Created March 19, 2012 14:51
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 podgorniy/2115058 to your computer and use it in GitHub Desktop.
Save podgorniy/2115058 to your computer and use it in GitHub Desktop.
Multiple class names search
/*
exaplme of use
has_class(document.body, 'main');
has_class(document.body, 'content wrapper js-on');
has_class(document.body, 'js-on content wrapper');
*/
function has_class (node, class_names) {
var classes, i;
classes = class_names.replace(/^\s+|\s+$/g, '').split(/\s+/);
if ( classes.length > 1 ) {
for (i = 0; i < classes.length; i += 1) {
if ( !has_class(node, classes[i]) ) {
return false;
}
}
} else {
if ( !node.className || (' ' + node.className + ' ').indexOf(' ' + classes[0] + ' ') === -1 ) {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment