0

Can you please take a look at This Demo and let me know why the toggle is not functioning well on checking and Un-checking the checkbox?

Here is the code I have:

$(document).ready(function(){   
    $('.checker').toggle(
        function () { 
            $('#check').attr('Checked','Checked'); 
        },
        function () { 
            $('#check').removeAttr('Checked'); 
        }
    );
});

Thanks

4 Answers 4

2

Since you are looking for switching the checked state there is no need to use .toggle(). Also use .prop() instead of .attr() to set the checked state

$(document).ready(function () {
    $('.checker').click(function () {
        $('#check').prop('checked', function (i, checked) {
            return !checked
        });
    });
});

Demo: Fiddle

.toggle() is removed in jQuery 1.9, so better do not use it any further

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

4 Comments

Thanks Arun, can you please also let me know how I can check the state of the checkbox in your method?
@Behseini to check $('#check').prop('checked', true), to uncheck $('#check').prop('checked', false)
sorry I just confused you what I mean by "check" getting the current state of the checkboc? I mean if it is checked or unchecked?
@Behseini if you want to read the state then var checked = $('#check').prop('checked'); or var checked = $('#check').is(':checked');
1

Try this

$(document).ready(function(){   
    $('.checker').toggle(
        function () { 
            $('#check').attr('Checked','Checked'); 
        },
        function () { 
            $('#check').prop('checked', false)
        }
    );
});

Updated fiddle: http://jsfiddle.net/57aq6/

Courtesy of this question: Remove attribute "checked" of checkbox

2 Comments

Thanks Daniel it works now but on small issue, How can I check the checkbox already have the attribute? I mean what if a user directly checked or uncheckd the checkbox? how can I add or remove the attr when it required?
I see what you mean, you should add some logic around whether it's checked or not. There are many questions on SO on this, checking if checkbox is checked.
1

Now it works I've updated it, the problem was in the attribute Checked and error in the HTML:

jQuery

$(document).ready(function(){   
    $('.checker').toggle(
        function () { 
            $('#check').attr('checked','checked'); 
        },
        function () { 
            $('#check').removeAttr('checked'); 
        }
    );
});

Here is another answer that covers the problem

Jquery Toggle Checkbox

Comments

1

Actually I don't know what is the problem with this code, but what I use to make this work is using:

    $('#check').attr('Checked',true); 

and

    $('#check').attr('Checked',false);

It works just fine.

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.