100

I want to check if a checkbox just got unchecked, when a user clicks on it. The reason for this is because i want to do a validation when a user unchecks a checkbox. Because atleast one checkbox needs to be checked. So if he unchecks the last one, then it automatically checks itself again.

With jQuery i can easily find out wether it's checked or not:

$('#check1').click(function() {
    if($(this).is(':checked'))
        alert('checked');
    else
        alert('unchecked');
});

But i actually only want to have an if statement that checks if a checkbox just got unchecked.

So i thought i could do that with the following code:

$('#check2').click(function() {
    if($(this).not(':checked'))
        alert('unchecked');
    else
        alert('checked');
});

But this will always show the 'unchecked' message. Not really what i was expecting...

demo: http://jsfiddle.net/tVM5H/

So eventually i need something like:

$('#check2').click(function() {
    if($(this).not(':checked')) {
        // Got unchecked, so something!!!
    }
});

But obviously this doesn't work. I rather don't want to use the first example, because then i'd have an unnecessary 'else' statement when i only need one 'if' statement.

So first thing, is this a jQuery bug? Cause to me it's unexpected behaviour. And second, anyone any ides for a good alternative?

0

9 Answers 9

198

Try this:

if(!$(this).is(':checked'))

demo

Sign up to request clarification or add additional context in comments.

Comments

72

The answer already posted will work. If you want to use the jQuery :not you can do this:

if ($(this).is(':not(:checked)'))

or

if ($(this).attr('checked') == false)

1 Comment

The first example in this answer seems the easiest to read out of all the options, so I vote for that.
9

jQuery to check for checked? Really?

if(!this.checked) {

Don't use a bazooka to do a razor's job.

4 Comments

If using jQuery to find the element, it should read $(this)[0].checked
@HoldOffHunger It's part of the native input element: developer.mozilla.org/en-US/docs/Web/HTML/Element/input/… If you are using jQuery then Chris has the correct syntax in the comment above yours.
@TheZ : Nice! Yes, you are right, answer by Chris is good. Suggestion: Add it to answer with credit? Both OP and top answers lean towards $(selector).is('...'), which is a pure jQuery solution. Only using .checked where they have .is('checked') will not work by itself.
If using native Javascript this will only display the initial value of the checkbox when the page is loaded. It will not reflect any state changes. Read the documentation here, developer.mozilla.org/en-US/docs/Web/HTML/Element/input/….
6
$(document).ready(function() {
        $("#check1").click(function() {
            var checked = $(this).is(':checked');
            if (checked) {
                alert('checked');
            } else {
                alert('unchecked');
            }
        });
    });

Comments

4
<script type="text/javascript">

 if(jQuery('input[id=input_id]').is(':checked')){
  // Your Statment
 }else{
  // Your Statment
 }

 OR

 if(jQuery('input[name=input_name]').is(':checked')){
  // Your Statment
 }else{
  // Your Statment
 }

</script>

Code taken from here : http://chandreshrana.blogspot.in/2015/10/how-to-check-if-checkbox-is-checked-or.html

Comments

2

Check out some of the answers to this question - I think it might apply to yours:

how to run click function after default behaviour of a element

I think you're running into an inconsistency in the browser implementation of the onclick function. Some choose to toggle the checkbox before the event is fired and some after.

2 Comments

This is actually really important too.
And that's why jQuery to check for checked. Really. ;)
1

The Answer already posted .But We can use the jquery in this way also

demo

$(function(){
    $('#check1').click(function() {
        if($('#check1').attr('checked'))
            alert('checked');
        else
            alert('unchecked');
    });

    $('#check2').click(function() {
        if(!$('#check2').attr('checked'))
            alert('unchecked');
        else
            alert('checked');
    });
});

Comments

0

Do it like this

if (typeof $(this).attr("checked") == "undefined" )

// To check if checkbox is checked
if( $(this).attr("checked")=="checked")

1 Comment

This could just be this.checked. The first line doesn't need typeof there (it will never raise a ReferencError). You should also be using prop() instead of attr(), which will return a boolean, making your second one also redundant.
0

Below code also will work

<input type="checkbox" id="check1" /> Check1

$('#check1').change(function() {
    if ($(this).is(":checked") == false)
    {
    alert("Unchecked");
    }
    else
    {
    alert("Checked");
    }    
});

or

$('#check1').change(function() {
   if ($(this).is(":not(:checked)"))
   {
   alert("Unchecked");
   }    
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.