Define an id
into the any checkbox
, like this:
<label class="checkbox-inline">
<input type="checkbox" id="" value="1"> information
</label>
<label class="checkbox-inline">
<input type="checkbox" id="" value="2"> information
</label>
<label class="checkbox-inline">
<input type="checkbox" id="" value="3"> information
</label>
<label class="checkbox-inline">
<input type="checkbox" id="any-checkbox" value="4"> any
</label>
I suggest this because it is not a good idea to use the fact that it is the fourth. If you add another checkbox in the future before it, then it will no longer be the fourth.
//We use .on to tackle with the dynamic nature of the HTML
$( "body" ).on( "change", ".checkbox-inline > input[type=checkbox]", function() {
if ($(this).attr("id") === "any-checkbox") { //Any checkbox tick
if ($(this).prop("checked")) { //User checked any
//Other checkboxes are unchecked
$(".checkbox-inline > input[type=checkbox]:not(#any-checkbox)").prop("checked", false)
}
} else { //Other checkbox tick
if ($(this).prop("checked")) {//User checked another checkbox
//Any checkbox is unchecked
$("#any-checkbox").prop("checked", false);
}
}
});
EDIT: As per the comment, I have solved the issue for multiple groups, like this:
<label class="checkbox-inline">
<input type="checkbox" id="" name="hairColor" value="1"> information
</label>
<label class="checkbox-inline">
<input type="checkbox" id="" name="hairColor" value="2"> information
</label>
<label class="checkbox-inline">
<input type="checkbox" id="" name="hairColor" value="3"> information
</label>
<label class="checkbox-inline">
<input type="checkbox" id="hairColor" name="hairColor" value="4" class="any"> any
</label>
<br>
<label class="checkbox-inline">
<input type="checkbox" id="" name="eyeColor" value="1"> eyeColor
</label>
<label class="checkbox-inline">
<input type="checkbox" id="" name="eyeColor" value="2"> eyeColor
</label>
<label class="checkbox-inline">
<input type="checkbox" id="" name="eyeColor" value="3"> eyeColor
</label>
<label class="checkbox-inline">
<input type="checkbox" id="eyeColor" name="eyeColor" class="any" value="4"> any
</label>
//We use .on to tackle with the dynamic nature of the HTML
$( "body" ).on( "change", ".checkbox-inline > input[type=checkbox]", function() {
if ($(this).hasClass("any")) { //Any checkbox tick
if ($(this).prop("checked")) { //User checked any
//Other checkboxes are unchecked
$(".checkbox-inline > input[type=checkbox][name=" + $(this).attr("name") + "]:not(.any)").prop("checked", false)
}
} else { //Other checkbox tick
if ($(this).prop("checked")) {//User checked another checkbox
//Any checkbox is unchecked
$("[name=" + $(this).attr("name") + "].any").prop("checked", false);
}
}
});