Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"+*)

like image 321
twitter Avatar asked Feb 24 '11 22:02

twitter


2 Answers

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/

like image 154
iwasrobbed Avatar answered Oct 09 '22 08:10

iwasrobbed


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

like image 27
John Weldon Avatar answered Oct 09 '22 09:10

John Weldon