I'm trying to ensure at least 1 checkbox in a group always remains checked, but I'm having difficulty.
I have multiple checkboxes whose name starts with post_type[
. The function runs for the on('change')
call just fine, with the problem occurring when I check to see if the input is a checkbox that is checked, where I get the following error.
value.is
is not a function
Can someone please help me troubleshoot my code? Thanks.
$(document).ready(function($){
$('input[name^="post_type["]', '#site-search').on('change', function(){
var checks = $('input[name^="post_type["]');
var one_checked = is_one_checked(checks);
if(one_checked === false){
$(this).attr('checked', 'true');
}
});
});
function is_one_checked(checks){
var checked = false; // Wheteher or not at least one box is checked (false by default)
$(checks).each(function(key, value){
if(value.is(':checkbox') && value.is(':checked')){
checked = true;
}
});
return checked;
}