Jquery toggle event is messing with checkbox value

Posted by John McCollum on Stack Overflow See other posts from Stack Overflow or by John McCollum
Published on 2008-12-10T10:34:22Z Indexed on 2010/04/05 6:03 UTC
Read the original article Hit count: 243

Filed under:
|
|

Hi all,

I'm using Jquery's toggle event to do some stuff when a user clicks a checkbox, like this:

$('input#myId').toggle(
function(){
//do stuff	
},
function(){
//do other stuff	
}
);

The problem is that the checkbox isn't being ticked when I click on the checkbox. (All the stuff I've put into the toggle event is working properly.)

I've tried the following:

$('input#myId').attr('checked', 'checked');

and

$(this).attr('checked', 'checked');

and even simply

return true;

But nothing is working. Can anyone tell me where I'm going wrong?

Edit - thanks to all who replied. Dreas' answer very nearly worked for me, except for the part that checked the attribute. This works perfectly (although it's a bit hacky)

$('input#myInput').change(function ()
{
    if(!$(this).hasClass("checked"))
    {
        //do stuff if the checkbox isn't checked
        $(this).addClass("checked");
        return;
    }

    //do stuff if the checkbox isn't checked
    $(this).removeClass('checked');
});

Thanks again to all who replied.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about forms