jQuery: Test if checkbox is NOT checked
JqueryIf StatementJquery Problem Overview
I'm having trouble figuring this out. I have two checkboxes (in the future will have more):
checkSurfaceEnvironment-1
checkSurfaceEnvironment-2
Basically, I want to write an if statement and test if one of them is checked and another is NOT checked. What's the easiest way to accomplish the following:
if ( $("#checkSurfaceEnvironment-1").attr('checked', true) &&
$("#checkSurfaceEnvironment-2").is('**(NOT??)** :checked') ) {
// do something
}
Jquery Solutions
Solution 1 - Jquery
One reliable way I use is:
if($("#checkSurfaceEnvironment-1").prop('checked') == true){
//do something
}
If you want to iterate over checked elements use the parent element
$("#parentId").find("checkbox").each(function(){
if ($(this).prop('checked')==true){
//do something
}
});
More info:
This works well because all checkboxes have a property checked which stores the actual state of the checkbox. If you wish you can inspect the page and try to check and uncheck a checkbox, and you will notice the attribute "checked" (if present) will remain the same. This attribute only represents the initial state of the checkbox, and not the current state. The current state is stored in the property checked of the dom element for that checkbox.
See https://stackoverflow.com/questions/6003819/properties-and-attributes-in-html
Solution 2 - Jquery
if (!$("#checkSurfaceEnvironment-1").is(":checked")) {
// do something if the checkbox is NOT checked
}
Solution 3 - Jquery
You can also use either jQuery .not()
method or :not()
selector:
if ($('#checkSurfaceEnvironment').not(':checked').length) {
// do stuff for not selected
}
Additional Notes
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.
Solution 4 - Jquery
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.
Solution 5 - Jquery
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
}
Solution 6 - Jquery
if ( $("#checkSurfaceEnvironment-1").is(":checked") && $("#checkSurfaceEnvironment-2").not(":checked") )
Solution 7 - Jquery
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.
Solution 8 - Jquery
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>
Solution 9 - Jquery
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
Solution 10 - Jquery
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;
}
Solution 11 - Jquery
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.
Solution 12 - Jquery
Simple and easy to check or unchecked condition
<input type="checkbox" id="ev-click" name="" value="" >
<script>
$( "#ev-click" ).click(function() {
if(this.checked){
alert('checked');
}
if(!this.checked){
alert('Unchecked');
}
});
</script>
Solution 13 - Jquery
There are two way you can check condition.
if ($("#checkSurfaceEnvironment-1").is(":checked")) {
// Put your code here if checkbox is checked
}
OR you can use this one also
if($("#checkSurfaceEnvironment-1").prop('checked') == true){
// Put your code here if checkbox is checked
}
I hope this is useful for you.
Solution 14 - Jquery
Here is the simplest way given
<script type="text/javascript">
$(document).ready(function () {
$("#chk_selall").change("click", function () {
if (this.checked)
{
//do something
}
if (!this.checked)
{
//do something
}
});
});
</script>
Solution 15 - Jquery
I used this and in worked for me!
$("checkbox selector").click(function() {
if($(this).prop('checked')==true){
do what you need!
}
});
Solution 16 - Jquery
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>");
}
Solution 17 - Jquery
if($("#checkbox1").prop('checked') == false){
alert('checkbox is not checked');
//do something
}
else
{
alert('checkbox is checked');
}
Solution 18 - Jquery
$("#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
}
});