I got an issue with a checkbox validation, I've been trying to much to create a validation with the checkboxes.
<script>
$('document').ready(function(){
let x = $('#TestChecks > input[type="checkbox"]').toArray();
$('#TestChecks').change(function() {
$('#TestChecks > input[type="checkbox"]').each((i,v) => {
console.log($('#TestChecks > input[type="checkbox"]:checked').length)
if($(x[0]).is(':checked')){
if(i != 0) {
$(x[i]).attr('checked', false)
$(x[i]).attr('disabled', true)
console.log('checked')
}
} else {
if(i != 0 ) {
$(x[i]).attr('checked', false)
$(x[i]).attr('disabled', false)
$(x[0]).attr('disabled', true)
$(x[1]).attr('checked', true)
console.log('unchecked')
} else if ($('#TestChecks > input[type="checkbox"]:checked').length == 0) {
$(x[0]).attr('disabled', false)
$(x[0]).attr('checked', true)
console.log('alone')
} else {
$(x[0]).attr('disabled', false)
$(x[0]).attr('checked', true)
}
}
});
}).trigger('change');
});
</script>
<div id="TestChecks">
<input type="checkbox" id="test1" value="test1"/>
<input type="checkbox" id="test2" value="test2"/>
<input type="checkbox" id="test3" value="test3"/>
</div>
1 - I need to create a validation when the first checkbox in the array is selected, disable the rest of the checkboxes like this. checkboxes first checked
2 - And when the first checkbox is unchecked, I need to enable the rest of the checkboxes and select the second one like this. checkbox 1 disabled 2 enabled
3 - And when there's nothing selected I need to check the first checkbox and disable the rest of the checkboxes like in the first validation
any help is welcome, thanks