116

I have some code

<input type="checkbox" id="chk" value="value" />
<label for="chk">Value </label>
<br/>
<input type="button" id="But1" value="set value" />
<br />
<input type="button" id="But2" value="read checked" />

javascript:

$(document).ready(function () {
    console.log("Ready ...");
    registerHandlers();

    function registerHandlers() {
        $('#But1').click(function () {
            $('#chk').prop('checked', !$('#chk').is(':checked'));
        });
        $('#But2').click(function () {
            var chk1 = $('#chk').is(':checked');
            console.log("Value : " + chk1);
        });

        $('input[type="checkbox"]').change(function () {
            var name = $(this).val();
            var check = $(this).prop('checked');
            console.log("Change: " + name + " to " + check);
        });
    }
});

How to handle change of checkbox using jQuery ? I need to put the handler to change any checkboxes checked.

[update]

There is a checkbox and a few buttons. Each button can change check box. How to catch an event changing the checkbox?

[Update]

I need handle change checkbox in this example jsfiddle. When I click on the box the message "OK" button is not shown.

6
  • I don't really understand your problem? What is supposed to happen when a checkbox is changed? Commented Feb 7, 2012 at 16:40
  • What's the problem? This code appears to work just fine Commented Feb 7, 2012 at 16:40
  • 2
    Could you please rephrase your question? You already have $('input[type="checkbox"]').change handler, whats wrong ? Commented Feb 7, 2012 at 16:41
  • If you change the button checkbox change events do not occur Commented Feb 7, 2012 at 16:43
  • 3
    FYI; $('#chk').prop('checked') returns a boolean rather then the value of the attribute. See api.jquery.com/prop Commented Feb 7, 2012 at 16:45

7 Answers 7

180

Use :checkbox selector:

$(':checkbox').change(function() {

        // do stuff here. It will fire on any checkbox change

}); 

Code: http://jsfiddle.net/s6fe9/

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

6 Comments

...and this.checked is very useful to determine wether the checkbox is checked or not.
Is it possible to register multiple handlers?
Is this still recommended in 2015?
It doesn't look like API has been changed, so yes.
this needs to be inside of $(document).ready(function ()... for me, is that correct?
|
78

You can use Id of the field as well

$('#checkbox1').change(function() {
   if($(this).is(":checked")) {
      //'checked' event code
      return;
   }
   //'unchecked' event code
});

Comments

18

Hope, this would be of some help.

$('input[type=checkbox]').change(function () {
    if ($(this).prop("checked")) {
        //do the stuff that you would do when 'checked'

        return;
    }
    //Here do the stuff you want to do when 'unchecked'
});

2 Comments

as of jQuery 1.6, better to use $(this).prop("checked"), as it will change dynamically with the actual state of the checkbox.
.attr("checked") isn't correct, because it isn't updated as the user clicks. I confirm @hrabinowitz's comment.
12
$("input[type=checkbox]").on("change", function() { 

    if (this.checked) {

      //do your stuff

     }
});

2 Comments

Please check your own code works before posting. There is a glaring issue with your selectors...
live() is deprecated.
6
$('#myForm').on('change', 'input[type=checkbox]', function() {
    this.checked ? this.value = 'apple' : this.value = 'pineapple';
});

2 Comments

Please elaborate your answer: code-only solutions are more likely to get deleted, because they don't explain, just fix (if any).
Sorry @rekaszeru I'll do it better next time
0

It seems to me removeProp is not working properly in Chrome : jsfiddle

        $('#badBut1').click(function () {
        checkit('Before');
        if( $('#chk').prop('checked') )
        {
          $('#chk').removeProp('checked');
        }else{
            $('#chk').prop('checked', true);
        }
        checkit('After');
    });
    $('#But1').click(function () {
        checkit('Before');
        if( $('#chk').prop('checked') )
        {
          $('#chk').removeClass('checked').prop('checked',false);
        }else{
            $('#chk').addClass('checked').prop('checked', true);
        }
        checkit('After');
    });

    $('#But2').click(function () {
        var chk1 = $('#chk').is(':checked');
        console.log("Value : " + chk1);
    });

    $('#chk').on( 'change',function () {
        checkit('Result');
    });
    function checkit(moment) {
        var chk1 = $('#chk').is(':checked');
        console.log(moment+", value = " + chk1);
    };

1 Comment

How to toggle a checkbox without event (from the outside) : jsfiddle.net/k91k0sLu/1
0

get radio value by name

  $('input').on('className', function(event){
        console.log($(this).attr('name'));
        if($(this).attr('name') == "worker")
            {
                resetAll();                 
            }
    });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.