1

I'm using a css class to define grouping in table rows:

<table>
    <tr class="otherClasses group1">...</tr>
    <tr class="otherClasses group1">...</tr>
    <tr class="otherClasses group2">...</tr>
    <tr class="otherClasses group2">...</tr>
    ...
    <tr class="otherClasses groupN">...</tr>
</table>

Now I need to loop through all the rows in my table and get groupX for each one. Then I'll split the string and extract group ID. What's the correct/best way to get groupX class among all tr classes only giving group prefix?

2 Answers 2

2

You should be able to use attribute contains selector in jQuery:

$('tr[class*="group"]')

Have a look at jquery selectors documentation for more info. You may need to tweak the value depending on your specifics (e.g. add a space before group).

0

The following will return an array with "group" classes:

var groupClasses = $(".otherClasses").map(function() {
    return (this.className.match(/group\d+/) || []).pop();
}).get();

Or in each() iteration:

$(".otherClasses").each(function() {
    var groupClass = (this.className.match(/group\d+/) || []).pop();
    // ...
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.