javascript - filter / sort results with multiple radio buttons with reset button jquery -
i have following fiddle showing filter stands.
i have filter working filters based on first radio button clicked out of 1 name
group. , second filter filters results within it's name
group.
where i'm stuck being able reset or 'show all' of 1 name
group while preserving second name
group.
it nice @ stage add more filters i'm not sure code structured correctly take that...
any appreciated!
here's js
var i, boxes, row, classnames = [], reset, results; $('.list').on('click', 'input', function(event){ // set defualt // reset = false; // title of row used later row = $(this).parents('.list').attr('data-attribute'); // hide results $('.results').find('.car').removeclass('active'); if( $(this).attr('value') == ('clear-' + row) ) { console.log('reset button: ' + row); //reset = true; } else { // set false $(this).parents('.list[data-attribute="' + row + '"]').find('input').attr('data-filter', 'false'); // set current clicked true $(this).attr('data-filter', 'true'); }; // go list inputs data-filter="true" // incase there have been previous clicks classnames = $(this).parents('.list-wrap').find('input[data-filter="true"]'); $.each(classnames, function(index, classname) { classname = $(classname).attr('id'); console.log(classname); console.log(index); // nice allow larger loop (if more filters added in future) // basing following statement on array size of 2 // filtering down filterd results // if(index !== classnames.length) didn't work if(index === 0) { results = $('.results').find('.' + classname); results.addclass('active'); } else { results = $('.results').find(':not(.active.' + classname + ')'); console.log(results); results.removeclass('active'); console.log('index not 1! see. ' + index); } }); });
and html
<div class="car active ford green"> <h4>type: ford</h4> <h5>color: green</h5> <p>lorem.</p> </div> <div class="car active ford blue"> <h4>type: ford</h4> <h5>color: blue</h5> <p>lorem.</p> </div> <div class="car active ford blue"> <h4>type: ford</h4> <h5>color: blue</h5> <p>lorem.</p> </div> <div class="car active audi green"> <h4>type: audi</h4> <h5>color: green</h5> <p>lorem.</p> </div> <div class="car active audi blue"> <h4>type: audi</h4> <h5>color: blue</h5> <p>lorem.</p> </div> <div class="car active jeep blue"> <h4>type: jeep</h4> <h5>color: blue</h5> <p>lorem.</p> </div> <div class="car active jeep green"> <h4>type: jeep</h4> <h5>color: green</h5> <p>lorem.</p> </div> <div class="car active jeep blue"> <h4>type: jeep</h4> <h5>color: blue</h5> <p>lorem.</p> </div>
managed figure way out reset , solve above issue.
here's fiddle if has similar issues.
here's js changed / added.
if( $(this).attr('value') == ('clear-' + row) ) { resetnames = $('.list[data-attribute="' + row + '"]').find('input:not([id="clear-' + row + '"])'); $.each(resetnames, function(i, resetname) { resetname = $(resetname).attr('id'); $('.results').find('.' + resetname).removeclass('active'); }); } else { // set current clicked true $(this).attr('data-filter', 'true'); }; // go list inputs data-filter="true" // incase there have been previous clicks classnames = $(this).parents('.list-wrap').find('input[data-filter="true"]'); // if data-filter="false" exit , reset results // means filter name group has been set 'clear' if(classnames.length === 0) { $('.results').find('.car').addclass('active'); // exit return; } $.each(classnames, function(index, classname) { ..../