I have 3 checkbox's with 8 different combinations and outcomes.
- Selecting no checkboxes shows no divs.
- Selecting one checkbox shows the colored div.
For example; clicking the red checkbox. - Selecting two checkboxes shows the color1+color2 div. But doesn't show either of the single colored divs.
For example; clicking the red and green checkboxes shows the redgreen div. But ensures both the red and green div boxes are hidden so there's only ever one div box showing. - Selecting all the checkboxes shows the rgb div. And doesn't show any other divs.
It does work I was wondering if this can be wrote in a simpler fashion?
$('input[type="checkbox"]').click(function () {
$('.box').hide();
if ($('#red').is(':checked')) $('.red.box').show();
if ($('#green').is(':checked')) $('.green.box').show();
if ($('#blue').is(':checked')) $('.blue.box').show();
if ($('#red').is(':checked') && $('#green').is(':checked')) $('.redgreen.box').show() && $('.red.box, .green.box').hide();
if ($('#red').is(':checked') && $('#blue').is(':checked')) $('.redblue.box').show() && $('.red.box, .blue.box').hide();
if ($('#green').is(':checked') && $('#blue').is(':checked')) $('.greenblue.box').show() && $('.green.box, .blue.box').hide();
if ($('#red').is(':checked') && $('#green').is(':checked') && $('#blue').is(':checked')) $('.rgb.box').show() && $('.red.box, .green.box, .greenblue, .redblue, .redgreen.box, .blue.box').hide();
});
.box {
padding: 20px;
display: none;
margin-top: 20px;
border: 1px solid #000;
}
.redblue .redgreen .greenblue {
border: 1px solid black;
}
.rgb {
border: 1px dashed black;
}
.red {
background: #ff0000;
}
.green {
background: #00ff00;
}
.blue {
background: #0000ff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<label>
<input type="checkbox" id="red" name="colorCheckbox" value="red">red</label>
<label>
<input type="checkbox" id="green" name="colorCheckbox" value="green">green</label>
<label>
<input type="checkbox" id="blue" name="colorCheckbox" value="blue">blue</label>
</div>
<div class="red box"><strong>red</strong></div>
<div class="green box"><strong>green</strong></div>
<div class="blue box"><strong>blue</strong></div>
<div class="redgreen box"><strong>red green</strong></div>
<div class="redblue box"><strong>red blue</strong></div>
<div class="greenblue box"><strong>green blue</strong></div>
<div class="rgb box"><strong>rgb</strong></div>