I have a button and 3 checkboxes, after button click I want to show a message if one or more of these checkboxs is checked. I prefer Jquery code if it's possible.
Thanks and any assistance it would be greatly appreciated.
$(function() {
$(".btn").click(function() {
if ($('input[type="checkbox"]').is(":checked")) {
if ($('input[name="box-1"]').is(":checked")) {
$(".message").text("Box-1 is checked");
}
if ($('input[name="box-2"]').is(":checked")) {
$(".message").text("Box-2 is checked");
}
if ($('input[name="box-3"]').is(":checked")) {
$(".message").text("Box-3 is checked");
}
} else {
$(".message").text("No box is checked");
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" name="box-1">
<input type="checkbox" name="box-2">
<input type="checkbox" name="box-3">
<p class="message"></p>
<button class="btn">CLICK ME</button>
My code works fine, but the problem is the message shows only one checkbox is checked and I want to show for example:
If box-1 and box-3 are checked, message will be "box-1 and box-3 are cheched" and the same way with other checkboxes. Thanks and any assistance it would be greatly appreciated.
