I\'m having trouble figuring this out. I have two checkboxes (in the future will have more):
checkSurfaceEnvironment-1
checkSurface
$("#chkFruits_0,#chkFruits_1,#chkFruits_2,#chkFruits_3,#chkFruits_4").change(function () {
var item = $("#chkFruits_0,#chkFruits_1,#chkFruits_2,#chkFruits_3,#chkFruits_4");
if (item.is(":checked")==true) {
//execute your code here
}
else if (item.is(":not(:checked)"))
{
//execute your code here
}
});
To do it with .attr()
like you have, to see if it's checked it would be .attr("checked", "checked")
, and if it isn't it would be .attr("checked") == undefined
try this one
if ($("#checkSurfaceEnvironment-1:checked").length>0) {
//your code in case of NOT checked
}
In Above code selecting the element by Id (#checkSurfaceEnvironment-1)
then filter out it's checked state by (:checked)
filter.
It will return array of checked element object. If there any object exists in the array then if condition will be satisfied.
I think the easiest way (with jQuery) to check if checkbox is checked or NOT is:
if 'checked':
if ($(this).is(':checked')) {
// I'm checked let's do something
}
if NOT 'checked':
if (!$(this).is(':checked')) {
// I'm NOT checked let's do something
}
Return true if all checbox are checked in a div
function all_checked (id_div){
all_checked = true;
$(id_div+' input[type="checkbox"]').each(function() {
all_checked = all_checked && $('this').prop('checked');
}
return all_checked;
}
if ( $("#checkSurfaceEnvironment-1").is(":checked") && $("#checkSurfaceEnvironment-2").not(":checked") )