Paul Attuck
Paul Attuck

Reputation: 2269

add class with jQuery for ends

i have:

<span id="asdfasdf_test">
<span id="adfaf_test33">
<span id="2342_test">
<span id="34223sdf_testrr">
<span id="asdfsdsdf_test">
<span id="asdf2343sdf_test">

.red {
color: red
}

if span id ends at _test i would like add for this span class .red . how can i make this with jQuery?

LIVE EXAMPLE: http://jsfiddle.net/X6TTd/

Upvotes: 3

Views: 116

Answers (2)

NE555
NE555

Reputation: 76

CSS can do that for you! Why do you want to use jQuery? (besides: jQuery understands this CSS Selector)

Here you have it: http://www.w3.org/TR/selectors/#selectors

Upvotes: 3

Chris Pratt
Chris Pratt

Reputation: 239470

$('span[id$="_test"]').addClass('red');

The $= attribute selector means 'ends with'. I added "span" to the jQuery selector since they are all spans in your example, but you can also select the attribute on any element:

$('[id$="_test"]')

Upvotes: 8

Related Questions