0

On html page I have multiple elements like,

<a class="myInput red  selected" selectable="">1</a>
<a class="myInput blue selected" selectable="">2</a>

point is that I could have many different myInput classes like red, blue, green, ... Now I want on certain event to remove all selected classes from entire document.

I know that I should use .removeClass("selected") but I dont know how to apply on whole document to many elements

1
  • as long as they have that class it will be removed Commented Sep 17, 2015 at 7:25

4 Answers 4

6

You can find all .selected elements and remove the class like this:

$('.selected').removeClass('selected');

If there are multiple .selected elements found in the document jQuery will internally loop over all of them for you and change the class.

Sign up to request clarification or add additional context in comments.

Comments

3

You can do this:

$('.myInput.selected').removeClass('selected');

it only removes the selected css class from the myinputs and which has the class selected.

Comments

1

You can use each loop like this as well :-

$('.selected').each(function(){
    $(this).removeClass("selected");
});

1 Comment

why loop each element when you can just do $('.selected').removeClass('selected');?
1

You can check with the code.

$(selector).removeAttr(attribute)

you can refer the below link too.

Link

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.