I am creating a very simple filter system where I want to have html checkboxes that can show and hide elements of certain categories.
The problem is that the checkboxes remain checked no matter what.
Jsfiddle example: http://jsfiddle.net/Jmnwr/
HTML:
<ul id="filters"> <li><input type="checkbox" checked="checked" value="categorya" id="filter-categorya" /><label for="filter-categorya">Category A</label></li> <li><input type="checkbox" checked="checked" value="categoryb" id="filter-categoryb" /><label for="filter-categoryb">Category B</label></li> </ul> <div class="categorya"></div> <div class="categorya"></div> <div class="categorya"></div> <div class="categorya"></div> <div class="categoryb"></div> <div class="categoryb"></div> <div class="categoryb"></div>
JS:
$("#filter-categorya").toggle( function() { $('.categorya').stop().fadeTo('slow', 0.2); $('#filter-categorya').removeAttr('checked'); }, function() { $('.categorya').stop().fadeTo('slow', 1.0); $('#filter-categorya').attr('checked', 'checked'); }); $("#filter-categoryb").toggle( function() { $('.categoryb').stop().fadeTo('slow', 0.2); $('#filter-categoryb').removeAttr('checked'); }, function() { $('.categoryb').stop().fadeTo('slow', 1.0); $('#filter-categoryb').attr('checked', 'checked'); });
I saw two posts on this topic, but I'm not sure if they are completely related or why my code in particular does not work. I also tried using the preventdefault function, but did nothing:
Cannot control flag status - jquery Why won't the .attr ('checked', 'checked') parameter be set?
Any help is always appreciated, thanks!
waffl source share