Skip to content

Instantly share code, notes, and snippets.

@helloluis
Created September 30, 2011 03:48
Show Gist options
  • Save helloluis/1252589 to your computer and use it in GitHub Desktop.
Save helloluis/1252589 to your computer and use it in GitHub Desktop.
Mini-plugin for selecting elements with regex instead of an actual class name
$.fn.findClassWithRegex = function(regex) {
var results = [];
$(this).each(function(){
if ($(this).attr('class').length>0 && $(this).attr('class').match(regex)) {
results.push( this );
}
});
return results;
};
@rstacruz
Copy link

Possibly better implementation:

$.expr[':'].classmatch = function(object,i,attrs) { return !! $(object).attr('class').match(attrs[3]); };
$("#links a:classmatch(^active)");
$("#links").find("a:classmatch(^active)");

@helloluis
Copy link
Author

Thanks, that does look more attractive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment