TeTe
TeTe

Reputation: 227

find elements by class whose id contains a substring and change the style

I have a set of elements with a style (stripe) and I need to find, in this set, those elements whose id contains a substring (element_A59) and this subset I need to change the style (stripe_new) and the others remove their style (stripe).

I used this jquery sentence, but it didn't work:

$('.stripe').find("td[id*=element_A59]").removeClass('stripe').addClass('stripe_new');

Upvotes: 0

Views: 723

Answers (2)

Milind Anantwar
Milind Anantwar

Reputation: 82231

You can rather use attribute contains selector along with class selector to get the desired elements:

$('td[id*=element_A59].stripe').removeClass('stripe').addClass('stripe_new');

Upvotes: 1

BenM
BenM

Reputation: 53198

Without seeing your markup (for example is .stripe defined on the td itslef?), it's difficult to answer your question.

find() will try to locate child elements. It looks like you're actually looking for the IDs of the matched elements.

You should probably use the filter() function:

$('.stripe').filter('[id*="element_A59"]').removeClass('stripe').addClass('stripe_new');

Here's what filter() does:

Description: Reduce the set of matched elements to those that match the selector or pass the function's test.

So essentially, by passing the substring selector to filter(), the matched td.stripe elements will be reduced to those whose ID attribute contains element_A59.

Upvotes: 2

Related Questions