I have question quite similar to javascript-php-add-checkbox-values-to-input-text
There is a parent checkbox, with 6 consecutive child checkbox. I've created a script when child checkbox checked, parent checkbox will be checked to. And if all 6 child checkboxes cleared, parent checkbox will be cleared also. Is there any way to input (and remove) these multiple check action from input text, since currently its only recognize one checkbox clicked. Im using latest jquery.
HTML
<input type="checkbox" name="chkcc5" id="chkGrpSD3" value="sd">SD
<input type="checkbox" name="chk5[]" class="chkGrpSD3" value="kelas 1">Kelas 1
<input type="checkbox" name="chk5[]" class="chkGrpSD3" value="kelas 2">Kelas 2
<input type="checkbox" name="chk5[]" class="chkGrpSD3" value="kelas 3">Kelas 3
<input type="checkbox" name="chk5[]" class="chkGrpSD3" value="kelas 4">Kelas 4
<input type="checkbox" name="chk5[]" class="chkGrpSD3" value="kelas 5">Kelas 5
<input type="checkbox" name="chk5[]" class="chkGrpSD3" value="kelas 6">Kelas 6
<input type="text" name="modelos" />
JQuery
$("input.chkGrpSD3").click(function () {
var flag = $('#chkGrpSD3').is(':checked');
if (flag === false) {
$('#chkGrpSD3').prop('checked', true);
}
var noOneChecked = $('input[name="chk5[]"]:checked').length === 0;
if (noOneChecked) {
$('#chkGrpSD3').prop('checked', false);
}
});
$("#chkGrpSD3").click(function () {
if (!this.checked) {
$('input.chkGrpSD3').prop('checked', false);
}
});
$(":checkbox").click(function () {
var delimiter = ";";
var checkbox = $(this);
var text = $("input[name='modelos']");
if (checkbox.is(":checked")) {
text.val(text.val() + checkbox.val() + delimiter)
} else {
text.val(function () {
return $(this).val().replace(checkbox.val() + delimiter, "");
});
}
});