javascript - Add class to all elements which have a certain class when clicked on -
i add class named "group" elements have class "someclass" - , e.g.:
<div class="someclass">sometext</div> <div class="someclass">someothertext</div> <div class="someotherclass">somemoretext</div> <div class="someotherclass">somemoreothertext</div>
should become following if user clicks on div containing class "someclass":
<div class="someclass group">sometext</div> <div class="someclass group">someothertext</div> <div class="someotherclass">somemoretext</div> <div class="someotherclass">somemoreothertext</div>
if user clicks on div containing "someotherclass" should added class "group".
i tried following jquery - unsuccessfully:
$("#someid").live("click", function() { var selectedclass; selectedclass = $(this).attr('class'); $('.'+selectedclass).addclass('group'); //...followed more jquery... });
i tried luck with:
$("#someid").live("click", function() { $(this).attr('class').addclass('group'); //...followed more jquery... });
...which didn't work either :(
you ought take account possibility of divs having more 1 class -- happen whenever div clicked twice in row:
$('div').on('click',function(e) { var cl = $(this).attr('class'), carr = cl.split(/\s+/); (var i=0; i<carr.length; i++) { $('div.'+carr[i]).addclass('newclass'); }; });
Comments
Post a Comment