3

I have a simple example:

<li id="item0" class="test"> ..... <li id="item10" class="test"> 

How to remove class "test" from all elements this id =~ "#item\d{1,}" using jquery ?

1

4 Answers 4

6

Use the attribute starts with selector to get just those IDs:

$('li[id^="item"]').removeClass('test'); 
Sign up to request clarification or add additional context in comments.

1 Comment

Good call. Beat me by a minute or so. ;)
1
$(youritem).removeClass('test'); 

3 Comments

I don't think removeAttr would work. removeClass is what you want.
You should use removeClass anyway :D
I think the question is how to use the regex as a selector, not how to remove the class...
1

I believe you would need a ^= selector on the element.

$('li[id^=item]').removeClass('test'); 

Check the jsfiddle

Comments

1

If you want to use regex expressions in your jQuery selectors, check out this plugin: http://james.padolsey.com/javascript/regex-selector-for-jquery/

In this context it would be something like:

$('.test:regex(id, ^item([0-9]+)$)').removeClass('test'); 

See jsFiddle for demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.