How to handle change of checkbox using jQuery?

JqueryCheckbox

Jquery Problem Overview


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.

Jquery Solutions


Solution 1 - Jquery

Use :checkbox selector:

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

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

}); 

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

Solution 2 - Jquery

You can use Id of the field as well

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

Solution 3 - Jquery

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'
});

Solution 4 - Jquery

$("input[type=checkbox]").on("change", function() { 
    
    if (this.checked) {
      
      //do your stuff
      
     }
});

Solution 5 - Jquery

$('#myForm').on('change', 'input[type=checkbox]', function() {
    this.checked ? this.value = 'apple' : this.value = 'pineapple';
});

Solution 6 - Jquery

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);
    };

Solution 7 - Jquery

get radio value by name

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBILLView Question on Stackoverflow
Solution 1 - JquerySamichView Answer on Stackoverflow
Solution 2 - JqueryPinkesh SharmaView Answer on Stackoverflow
Solution 3 - JqueryLokesh YadavView Answer on Stackoverflow
Solution 4 - JqueryArjunView Answer on Stackoverflow
Solution 5 - JqueryEverton Z. P.View Answer on Stackoverflow
Solution 6 - JqueryGlaubuleView Answer on Stackoverflow
Solution 7 - JquerySiva AnandView Answer on Stackoverflow