I am trying to create a selection of different checkboxes that should show content based on the selections that are chosen. I managed to create show/hide different text linked to each checkbox so far.
I am now wanting to show/hide a single image based on the selections that have been made. How to go about this? Create a function to count the checkboxes that then displays an image. Something like this:
If you select 1-3 boxes show the first image, hide all other images. If you select 4-6 show the second image, hide all other images. If you select 7-9 show the third image, hide all other images. If you select all of them show the fourth image, hide all other images.
The order the selection have made doesn't make a difference. I just need to count the number of selections that have made.
CSS:
.categorya,
.categoryb,
.categoryc,
.categoryd,
.categorye,
.categoryf,
.categoryg,
.categoryh,
.categoryi {
display: none;
font-size: 13px;
color: #000000;
font-family: sans-serif;
}
HTML:
<input id="filter-categorya" class="js-toggler" type="checkbox" value="categorya">
<label for="filter-categorya">Category A
</label>
<input id="filter-categoryb" class="js-toggler" type="checkbox" value="categoryb">
<label for="filter-categoryb">Category B
</label>
<input id="filter-categoryc" class="js-toggler" type="checkbox" value="categoryc">
<label for="filter-categoryc">Category C
</label>
<input id="filter-categoryd" class="js-toggler" type="checkbox" value="categoryd">
<label for="filter-categoryd">Category D
</label>
<input id="filter-categorye" class="js-toggler" type="checkbox" value="categorye">
<label for="filter-categorye">Category E
</label>
<input id="filter-categoryf" class="js-toggler" type="checkbox" value="categoryf">
<label for="filter-categoryf">Category F
</label>
<input id="filter-categoryg" class="js-toggler" type="checkbox" value="categoryg">
<label for="filter-categoryg">Category G
</label>
<input id="filter-categoryh" class="js-toggler" type="checkbox" value="categoryh">
<label for="filter-categoryh">Category H
</label>
<input id="filter-categoryi" class="js-toggler" type="checkbox" value="categoryi">
<label for="filter-categoryi">Category I</label>
<div class="categorya">A</div>
<div class="categoryb">B</div>
<div class="categoryc">C</div>
<div class="categoryd">D</div>
<div class="categorye">E</div>
<div class="categoryf">F</div>
<div class="categoryg">G</div>
<div class="categoryh">H</div>
<div class="categoryi">I</div>
<h1>Images:</h1>
<div>
<img alt="" class="js-toggle-image" src="http://dummyimage.com/600x400/000/fff&text=First+image">
<img alt="" class="js-toggle-image" src="http://dummyimage.com/600x400/000/fff&text=Second+image">
<img alt="" class="js-toggle-image" src="http://dummyimage.com/600x400/000/fff&text=Third+image">
<img alt="" class="js-toggle-image" src="http://dummyimage.com/600x400/000/fff&text=Fourth+image">
</div>
JS:
$('input').click(function() {
var category = $(this).val();
if (!$(this).attr('checked')) $('.' + category).hide();
else $('.' + category).show();
});