Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switching div class between divs (jquery, javascript)

I think this should be a pretty common jquery function but I just couldn't find anything about it.

say I have 10 divs and each div trigger an event when clicked. So when I cilck a div, jquery adds a class, say "clicked" to the div. But how can I set it so when I click another div, it removes the previous div class "clicked" and gives it to the div that I just clicked?

Thanks!!!

like image 240
Henry Avatar asked Jan 26 '26 05:01

Henry


1 Answers

$("div.groupname").click(function() {
    $(this).addClass("clicked").siblings().removeClass("clicked");
});

or:

$("div.groupname").click(function() {
    $(this).siblings().removeClass("clicked").end().addClass("clicked");
});

You can try it here.

like image 69
karim79 Avatar answered Jan 28 '26 18:01

karim79