115

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.

immutabl
  • 6,857
  • 13
  • 45
  • 76
BILL
  • 4,711
  • 10
  • 57
  • 96
  • I don't really understand your problem? What is supposed to happen when a checkbox is changed? – Niklas Feb 07 '12 at 16:40
  • What's the problem? This code appears to work just fine – JaredPar Feb 07 '12 at 16:40
  • 2
    Could you please rephrase your question? You already have $('input[type="checkbox"]').change handler, whats wrong ? – Madman Feb 07 '12 at 16:41
  • If you change the button checkbox change events do not occur – BILL Feb 07 '12 at 16:43
  • 3
    FYI; `$('#chk').prop('checked')` returns a boolean rather then the value of the attribute. See http://api.jquery.com/prop – Stefan Feb 07 '12 at 16:45
  • There is a checkbox and a few buttons. Each button can change check box. How to catch an event changing the checkbox? – BILL Feb 07 '12 at 16:51

7 Answers7

178

Use :checkbox selector:

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

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

}); 

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

Philzen
  • 3,945
  • 30
  • 46
Samich
  • 29,157
  • 6
  • 68
  • 77
76

You can use Id of the field as well

$('#checkbox1').change(function() {
   if($(this).is(":checked")) {
      //'checked' event code
      return;
   }
   //'unchecked' event code
});
Pinkesh Sharma
  • 2,428
  • 20
  • 16
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'
});
Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
Lokesh Yadav
  • 1,574
  • 5
  • 23
  • 50
  • 6
    as of jQuery 1.6, better to use $(this).prop("checked"), as it will change dynamically with the actual state of the checkbox. – hrabinowitz Feb 19 '14 at 20:55
  • 3
    .attr("checked") isn't correct, because it isn't updated as the user clicks. I confirm @hrabinowitz's comment. – Adrien Oct 27 '14 at 09:29
11
$("input[type=checkbox]").on("change", function() { 

    if (this.checked) {

      //do your stuff

     }
});
Arjun
  • 2,159
  • 1
  • 17
  • 26
6
$('#myForm').on('change', 'input[type=checkbox]', function() {
    this.checked ? this.value = 'apple' : this.value = 'pineapple';
});
Jonathan
  • 10,936
  • 8
  • 64
  • 79
Everton Z. P.
  • 371
  • 3
  • 8
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);
    };
Glaubule
  • 169
  • 1
  • 7
0

get radio value by name

  $('input').on('className', function(event){
        console.log($(this).attr('name'));
        if($(this).attr('name') == "worker")
            {
                resetAll();                 
            }
    });
Siva Anand
  • 2,872
  • 2
  • 18
  • 9