jQuery: Test if checkbox is NOT checked

how to check checkbox is checked or not in jquery on button click
how to check if checkbox is checked in jquery
jquery get checkbox value if checked
how to check multiple checkbox is checked or not in jquery
check if checkbox is checked javascript
check if checkbox is checked jquery using id
jquery checkbox uncheck
how to get checked and unchecked checkbox value in jquery

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
}

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 Properties and Attributes in HTML

Check if checkbox is NOT checked on click - jQuery, How do you check if a checkbox is not checked in jQuery? The jQuery prop() method provides a simple, effective, and reliable way to track down the current status of a checkbox. It works pretty well in all conditions because every checkbox has a checked property which specifies its checked or unchecked status. Do not misunderstand it with the checked attribute. The checked attribute only define the

if (!$("#checkSurfaceEnvironment-1").is(":checked")) {
    // do something if the checkbox is NOT checked
}

How to check whether a checkbox is checked in jQuery , How do you check if checkbox is checked or not? 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

You can also use either jQuery .not() method or :not() selector:

if ($('#checkSurfaceEnvironment').not(':checked').length) {
    // do stuff for not selected
}

JSFiddle Example


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.

<input type="checkbox">, How do you check if a checkbox is checked or not in selenium? jQuery check if an input is type checkbox? Ask Question Asked 10 years, 6 months ago. Check if checkbox is checked with jQuery. 2286. jQuery scroll to element.

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.

Check if a Checkbox is checked or not in Selenium, Using the jQuery :checked Selector. <script> $('input[type="checkbox"]'). click(function(){ if($(this). is(":checked")){ console.log("Checkbox is checked." ); } else if($(this). is(":not(:checked)")){ console.log("Checkbox is unchecked." ); } Using jQuery you can determine if a checkbox has been checked, performing an action based on the result. For example, you may want to conditionally show or hide form fields when a checkbox is selected. The best way to do this is to use is to use is(":checked").

if ( $("#checkSurfaceEnvironment-1").is(":checked") && $("#checkSurfaceEnvironment-2").not(":checked") )

How to Check a Checkbox is Checked or Not Using jQuery, prop() method like so: $('input[type=checkbox]'). prop('checked'); // Returns true if checked, false if unchecked. this.checked is not jQuery, as the OP asked for. Also, it only works when user clicks on the checkbox, which is not part of the question. The question is, again, How to check whether a checkbox is checked in jQuery? at any given time with or without clicking the checkbox and in jQuery. – Marc Compte Jul 6 '17 at 10:58 |

Check If A Checkbox Is Checked With jQuery, prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status. How do you check if a checkbox is checked with jQuery? No, it’s not a tongue-twister! Let’s say you’ve got this input: It’ll be checked by default because of the checked attribute. jQuery

How to Check if Checkbox is Checked or Not using jQuery, Using jQuery is() method, along with the :checked and :not() selector, you can easily check the status of a checkbox or a group of checkboxes on a web page. Check if a CheckBox is checked (selected) or not using jQuery The following HTML Markup consists of an HTML CheckBox and a Button. When the Button is clicked, a jQuery click event handler is executed which first references the CheckBox using its ID and then based on whether it is checked or unchecked, displays a JavaScript alert message box.

JQuery check if checkbox is NOT checked, Basically, I want to write an if statement and test if either are checked. What's the easiest way to accomplish this? (i.e if  Here you will learn jquery get checkbox value if checked. if you have question about how to check checkbox is checked or not in jquery using id then i will give simple example with solution. You just need to some step to done jquery check if checkbox is checked or not. We will use jquery prop() method to check checkbox is checked or not.

Comments
  • Use || not && to check if either is checked. && checks if both are checked.
  • Umm... this will just set checked = true
  • @Pablo - Check my answer below, which I is also a reliable/apt option.
  • @PabloMescher Hey, can you please help me? I actually want to determine which checkbox(es) is/are checked among several? And, enable/disable textbox next to it. Any help will be highly appreciated.
  • @1lastBr3ath sure, if you are using jQuery you just have to find a selector for the textbox relative to the checkbox and use my second example. It would look something like if($(this).prop('checked')) { $(this).find("<textboxSelector>").attr("disabled", true); }
  • @Stophface in this case this property will always be a boolean since it is a computed property and not a string, so you can get away with using ==. It is wise to always use === though since this is not always the case
  • !$("#checkSurfaceEnvironment").is(":checked") returns a boolean. It is true if the checkbox is not checked. $('#checkSurfaceEnvironment').not(':checked') returns an array of DOM elements. Testing this with 'if' returns 'true' even if there was no match to the selector and the array is empty. The only way your answer could be correct is if you tested i$('#checkSurfaceEnvironment').not(':checked').length, which will return 0 or 1.
  • Although your answer might help, you should at least explain why. As it is, your answer has been passed into the Low Quality Posts moderation queue (which is where I am viewing it) I suggest you edit your answer to add an explanation.
  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review