I\'m having trouble figuring this out. I have two checkboxes (in the future will have more):
checkSurfaceEnvironment-1
checkSurface
I was looking for a more direct implementation like avijendr suggested.
I had a little trouble with his/her syntax, but I got this to work:
if ($('.user-forms input[id*="chkPrint"]:not(:checked)').length > 0)
In my case, I had a table with a class user-forms
, and I was checking if any of the checkboxes that had the string checkPrint
in their id
were unchecked.
An alternative way:
Here is a working example and here is the code, you should also use prop.
$('input[type="checkbox"]').mouseenter(function() {
if ($(this).is(':checked')) {
//$(this).prop('checked',false);
alert("is checked");
} else {
//$(this).prop('checked',true);
alert("not checked");
}
});
I commented out the way to toggle the checked attribute.
if($("#checkbox1").prop('checked') == false){
alert('checkbox is not checked');
//do something
}
else
{
alert('checkbox is checked');
}
You can also use either jQuery .not() method or :not() selector:
if ($('#checkSurfaceEnvironment').not(':checked').length) {
// do stuff for not selected
}
JSFiddle Example
From the jQuery API documentation for the :not()
selector:
The .not() method will end up providing you with more readable selections than pushing complex selectors or variables into a :not() selector filter. In most cases, it is a better choice.
Here I have a snippet for this question.
$(function(){
$("#buttoncheck").click(function(){
if($('[type="checkbox"]').is(":checked")){
$('.checkboxStatus').html("Congratulations! "+$('[type="checkbox"]:checked').length+" checkbox checked");
}else{
$('.checkboxStatus').html("Sorry! Checkbox is not checked");
}
return false;
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<p>
<label>
<input type="checkbox" name="CheckboxGroup1" value="checkbox" id="CheckboxGroup1_0">
Checkbox</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup1_" value="checkbox" id="CheckboxGroup1_1">
Checkbox</label>
<br>
</p>
<p>
<input type="reset" value="Reset">
<input type="submit" id="buttoncheck" value="Check">
</p>
<p class="checkboxStatus"></p>
</form>
I know this has already been answered, but still, this is a good way to do it:
if ($("#checkbox").is(":checked")==false) {
//Do stuff here like: $(".span").html("<span>Lorem</span>");
}