I've multiple checkboxes which are populated from database, except one checkbox which is "All" (used to check/uncheck all other checkboxes onclick)
When all the options are checked and if any option other than 'All' is unchecked then checkbox of All should be unchecked.
When all the option are checked except 'All' then 'All' should be checked. How to proceed?
My code:
<script>
$(document).ready(function () {
('#check').append('<input type="checkbox" id="checkAll" name="myCheckbox[]" value="All" > </input>' + "All" );
//'datadb' is data from db in json array
//datadb={'apple','banana','orange'}
$.each(datadb, function(i, fruit) {
$('#check').append('<input type="checkbox" name="myCheckbox[]" class=".chk" value="' + fruit + '" > </input>' + fruit );
$('#check').append('<br/>');
}
});
</script>
<script>
$(document).ready(function() {
$("#checkAll").click(function () {
$('input:checkbox').not(this).removeProp('checked');
});
});
</script>
<div id="check" onchange="testingclick()"></div>
How to fill the function to satisfy above 1 and 2
<script>
function testingclick(){
var $check_values = $("input[type=checkbox][name='myCheckbox[]']:checked");
var $check_len = $check_values.length;
var $total_len = $("input[type=checkbox][name='myCheckbox[]']").length;
window.var_multiple_checked = $check_values.map(function(){ return "'" + this.value + "'"; }).get();
temp_checked= window.var_multiple_checked;
}
</script>