Francesca
Francesca

Reputation: 28128

jQuery remove check several ID's for one class name

I have a list of links, which when clicked get a class of "selected".

$("#" + filename).addClass("selected");

I have another button, which when clicked runs a different function. It also needs to remove all classes of selected from these links.

However there is no way to tell from the click of the button which link has the selected class so instead I need to check all of them for the class and remove it if it is present.

I expect the best way to do this would be to loop through all the link ID's and remove the selected class (so as not to repeat the code over and over).

So I guess, how could I combine the following into one loop?

I've prepared an array of all the link ID's called var linkIDs

var linkIDs = ["newpost", "getlink", "addpic","tags"];

$("#newpost").removeClass("selected");

Upvotes: 0

Views: 21

Answers (1)

James Donnelly
James Donnelly

Reputation: 128781

If the element has a class of "selected", you'll be able to select it with a class selector. You can simply:

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

Upvotes: 1

Related Questions