javascript - Toggle styling for active element in group of elements -
given group of elements no target attribute (e.g. following code), effiecient way set highlight-styling selected element while unsetting same styling selected element ?
<div id="uno" class="element_parent"> <a href="#" class="element">one</div> </div> <div id="dos" class="element_parent"> <a href="#" class="element">one</div> </div> <div id="tres" class="element_parent"> <a href="#" class="element">one</div> </div>
i add , remove class on clicking anchor, so:
$('.element').on('click', function(e) { e.preventdefault(); $(this).addclass('active') .closest('div') .siblings('div') .find('a') .removeclass('active') }); css
.active {color: red;} /* or whatever */ or:
$('.element').on('click', function(e) { e.preventdefault(); $('.element.active').removeclass('active'); $(this).addclass('active'); });
Comments
Post a Comment