twitter
twitter

Reputation: 9305

selecting classes using wildcard not exact class name

I have several classes that I want to select .group1-1 .group1-2 .group1-3, each one of these has 50 elements under it.

Is there a way to select all classes that start with group1 (so I end up selecting group1-1, group1-2, group1-3), something like $(".group1"+*)

Upvotes: 9

Views: 14046

Answers (2)

iwasrobbed
iwasrobbed

Reputation: 46703

You can also use something along the lines of this if you'd like to avoid regex:

$("[class^='group1-']").click(function () {
    var groupNumber = $(this).attr('class').split('-')[1];
    alert('Yep, you clicked group1-' + groupNumber); 
});

Example here: http://jsfiddle.net/iwasrobbed/7bjtb/

Upvotes: 13

John Weldon
John Weldon

Reputation: 40789

This question discusses jquery wildcard / regex selectors. Which basically allow you to use a regular expression to specify matching classes.

Upvotes: 2

Related Questions