Click toggle with jQuery

JqueryOnclickToggle

Jquery Problem Overview


I've used a hover function where you do x on mouseover and y and mouseout. I'm trying the same for click but it doesn't seem to work:

$('.offer').click(function(){ 
  $(this).find(':checkbox').attr('checked', true ); 
},function(){
  $(this).find(':checkbox').attr('checked', false ); 
});

I want the checkbox to be checked when clicking on a div, and unchecked if clicked again - a click toggle.

Jquery Solutions


Solution 1 - Jquery

This is easily done by flipping the current 'checked' state of the checkbox upon each click. Examples:

 $(".offer").on("click", function () { 
       var $checkbox = $(this).find(':checkbox');
       $checkbox.attr('checked', !$checkbox.attr('checked'));
 });

or:

 $(".offer").on("click", function () { 
       var $checkbox = $(this).find(':checkbox');
       $checkbox.attr('checked', !$checkbox.is(':checked'));
 });

or, by directly manipulating the DOM 'checked' property (i.e. not using attr() to fetch the current state of the clicked checkbox):

 $(".offer").on("click", function () { 
       var $checkbox = $(this).find(':checkbox');
       $checkbox.attr('checked', !$checkbox[0].checked);
 });

...and so on.

Note: since jQuery 1.6, checkboxes should be set using prop not attr:

 $(".offer").on("click", function () { 
       var $checkbox = $(this).find(':checkbox');
       $checkbox.prop('checked', !$checkbox[0].checked);
 });

Solution 2 - Jquery

Another approach would be to extended jquery like this:

$.fn.toggleCheckbox = function() {
    this.attr('checked', !this.attr('checked'));
}

Then call:

$('.offer').find(':checkbox').toggleCheckbox();

Solution 3 - Jquery

Warning: using attr() or prop() to change the state of a checkbox does not fire the change event in most browsers I've tested with. The checked state will change but no event bubbling. You must trigger the change event manually after setting the checked attribute. I had some other event handlers monitoring the state of checkboxes and they would work fine with direct user clicks. However, setting the checked state programmatically fails to consistently trigger the change event.

jQuery 1.6

$('.offer').bind('click', function(){ 
    var $checkbox = $(this).find(':checkbox');
    $checkbox[0].checked = !$checkbox[0].checked;
    $checkbox.trigger('change'); //<- Works in IE6 - IE9, Chrome, Firefox
});

Solution 4 - Jquery

You could use the toggle function:

$('.offer').toggle(function() {
    $(this).find(':checkbox').attr('checked', true);
}, function() {
    $(this).find(':checkbox').attr('checked', false);
});

Solution 5 - Jquery

Why not in one line?

$('.offer').click(function(){
    $(this).find(':checkbox').attr('checked', !$(this).find(':checkbox').attr('checked'));
});

Solution 6 - Jquery

I have a single checkbox named chkDueDate and an HTML object with a click event as follows:

$('#chkDueDate').attr('checked', !$('#chkDueDate').is(':checked'));

Clicking the HTML object (in this case a <span>) toggles the checked property of the checkbox.

Solution 7 - Jquery

jQuery: Best Way, delegate the actions to jQuery (jQuery = jQuery).

$( "input[type='checkbox']" ).prop( "checked", function( i, val ) {
    return !val;
});

Solution 8 - Jquery

try changing this:

$(this).find(':checkbox').attr('checked', true ); 

to this:

$(this).find(':checkbox').attr('checked', 'checked'); 

Not 100% sure if that will do it, but I seem to recall having a similar problem. Good luck!

Solution 9 - Jquery

$('.offer').click(function(){ 
	if ($(this).find(':checkbox').is(':checked'))
	{
		$(this).find(':checkbox').attr('checked', false); 
	}else{
		$(this).find(':checkbox').attr('checked', true); 
	}
});

Solution 10 - Jquery

In JQuery I don't think that click() accepts two functions for toggling. You should use the toggle() function for that: http://docs.jquery.com/Events/toggle

Solution 11 - Jquery

$('.offer').click(function() { 
    $(':checkbox', this).each(function() {
        this.checked = !this.checked;
    });
});

Solution 12 - Jquery

<label>
    <input
        type="checkbox"
        onclick="$('input[type=checkbox]').attr('checked', $(this).is(':checked'));"
    />
    Check all
</label>

Solution 13 - Jquery

Easiest solution

$('.offer').click(function(){
	var cc = $(this).attr('checked') == undefined  ? false : true;
	$(this).find(':checkbox').attr('checked',cc);
});

Solution 14 - Jquery

Another alternative solution to toggle checkbox value:

<div id="parent">
    <img src="" class="avatar" />
    <input type="checkbox" name="" />
</div>


$("img.avatar").click(function(){

    var op = !$(this).parent().find(':checkbox').attr('checked');
    $(this).parent().find(':checkbox').attr('checked', op);

});

Solution 15 - Jquery

    $('controlCheckBox').click(function(){
	var temp = $(this).prop('checked');
	$('controlledCheckBoxes').prop('checked', temp);
});

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
QuestioneozzyView Question on Stackoverflow
Solution 1 - Jquerykarim79View Answer on Stackoverflow
Solution 2 - JqueryJustin TannerView Answer on Stackoverflow
Solution 3 - JqueryTim SantefordView Answer on Stackoverflow
Solution 4 - JquerydcharlesView Answer on Stackoverflow
Solution 5 - JqueryThomas SeresView Answer on Stackoverflow
Solution 6 - JqueryRoss MehlmanView Answer on Stackoverflow
Solution 7 - JquerymiosserView Answer on Stackoverflow
Solution 8 - JquerybrettkellyView Answer on Stackoverflow
Solution 9 - Jquerylod3nView Answer on Stackoverflow
Solution 10 - JqueryKaiView Answer on Stackoverflow
Solution 11 - JqueryAlex BarrettView Answer on Stackoverflow
Solution 12 - JquerydobsView Answer on Stackoverflow
Solution 13 - JquerykeithicsView Answer on Stackoverflow
Solution 14 - JquerySaeedView Answer on Stackoverflow
Solution 15 - JquerymahmohView Answer on Stackoverflow