admin管理员组

文章数量:1392066

Hi i have a dynamic generated div's list with unique id's for every generated div's,

when click on one of the dynamic generated div this will change the selected div background color by this code $(this).addClass('add_color'); if again i click on another one div then the older selected div background color should change to default so i tried this code $(".add_color").removeClass(".add_color"); but it's not working, please help.

Hi i have a dynamic generated div's list with unique id's for every generated div's,

when click on one of the dynamic generated div this will change the selected div background color by this code $(this).addClass('add_color'); if again i click on another one div then the older selected div background color should change to default so i tried this code $(".add_color").removeClass(".add_color"); but it's not working, please help.

Share Improve this question edited Dec 18, 2014 at 7:00 asked Dec 18, 2014 at 6:47 user4246994user4246994 5
  • you should be sharing relevant code, what needs to be done and what you have tried. – Milind Anantwar Commented Dec 18, 2014 at 6:48
  • jquery uses events, so the question is when you need to delete them? – bto.rdz Commented Dec 18, 2014 at 6:49
  • @AshokSri, do use some preffix/suffix for id e.g. foo1, foo2 etc? – user2575725 Commented Dec 18, 2014 at 6:52
  • You can specify a separate attribute, say indicator="Y" for the generated divs and then while removing the classes, you can do: $("div[indicator='Y']").removeClass("your_class") – ArinCool Commented Dec 18, 2014 at 6:52
  • hi all, i've edited my question please understand the situation now – user4246994 Commented Dec 18, 2014 at 7:00
Add a ment  | 

4 Answers 4

Reset to default 5
$(".className").removeClass("className")

You need to modify the click handler to add/remove class on click of divs:

$('body').on('click','.somedivs',function(){
   $(".add_color").not($(this)).removeClass("add_color");
   $(this).addClass('add_color');
});

if you just need to remove the class for any div containing that particular class

$("div").removeClass('someClass'); 
$(".your_class").removeClass("your_class");

本文标签: javascripthow to remove class of a div without knowning id of divStack Overflow