javascript - Combining removeClass() and data() to remember which elements had the class and restore it -
so asked similar question few hours later when tried use couldn't figure out how use it. told following:
$(element).removeclass('active'); $(element).data('removed-class', 'active');
further can fetched using
var removeclass = $(element).data('removed-class');
so "clean" way function?
so basically, if have 7 <li>
rows, , randomly 3 of <li>
have class "active".
now, if execute function performs remove "active" classes <li>
$("li").removeclass("active")
example, how can restore same "active" <li>
classes had class removed?
cheers
give shot, , see if fits bill.
var $listitems = $('li','#wrapper'); function clstodata (elements, cls) { $(elements).each(function(){ var $this = $(this); if($this.hasclass(cls)){ $this.removeclass(cls).data('removed-class',cls); } }); } function restoreremovedclasses (elements) { $(elements).each(function(){ var $this = $(this); $this.addclass($this.data('removed-class')); }); } $('button','#buttons').on('click',function(){ var $this = $(this); switch ($this.attr('id')){ case "button1": clstodata($listitems,'active'); break; case "button2": restoreremovedclasses ($listitems); break; } });
Comments
Post a Comment