$('document').ready(function(){
$('[name=mycheckbox]').live('click', function(){
if($(this).is(':checked'))
{
alert('it is checked');
}
else
{
alert('it is not checked');
}
});
$('[name=mycheckbox]').click();
});
If the checkbox is checked and you click it, the alert box says, “it is not checked”, but when the page runs and the click event is fired (with the checkbox checked), the alert box says, “it is checked”. Why? Is the state of the checkbox not effected by the click event? Is it mousedown that changes the state?
Instead of
clickyou should use thechangeevent here, like this:And invoke it with the same trigger, like this:
The
clickis separate from thechange, if you want the event to fire when the check actually has finished changing, then you wantchange, notclick. Alternately, if you want to toggle it from it’s initial state still, do this: