Conceptually, achieving this solely with CSS is possible by utilizing the :checked
selector alongside the +
sibling selector. The CSS code for this approach would resemble the following:
.checkbox:checked + label + .checkbox:checked + label + .checkbox:checked + label + .checkbox:checked + label + .checkbox:checked + label + #the-button {
display: block;
}
While theoretically feasible, I encountered unexpected behavior when testing it on jsFiddle. Moreover, this method is not very practical as it can be challenging to troubleshoot and necessitates a specific HTML structure.
In practice, accomplishing this task would require only a few lines of jQuery. Here's an example of the code I devised:
// cache the collection of checkboxes
var $checkboxes = $('#js-solution .checkbox');
// when a checkbox changes
$checkboxes.change(function() {
// filter out the checked checkboxes
var $checked = $checkboxes.filter(':checked');
// if there are 5 checked checkboxes
if ($checked.length == 5) {
// display the button
$('#the-js-button').css('display', 'block');
// if there are not 5 checked checkboxes
} else {
// hide the button
$('#the-js-button').css('display', 'none');
}
});
The comments included should clarify the functioning of the code, but feel free to seek clarification.
I have juxtaposed both solutions in this fiddle: http://jsfiddle.net/7GN7q/1/
The CSS solution did not yield the desired outcome for me. However, it appears to be Chrome-specific, considering that inspecting the style reveals the application of the rule. Interestingly, toggling display:block
in the inspector makes it function correctly. For practical purposes, I would opt for the JavaScript solution.