Rupam Datta
Rupam Datta

Reputation: 1889

Is there an event for checkbox changed state?

Is there an event for checkbox change when the state is changed/modified based on some other interaction. I mean not by clicking on the text box.

I've tried this code:

$('input[type="checkbox"]').bind('change',function(){
    alert('Checkbox state is changed.');
});

...but as you can see in this demo, if I click the checkbox I see an alert showing "Checkbox state is changed." message triggered by the change event. But the same is not triggered when the checkbox state is changed using the button.

Am I missing anything?

Appreciate any help.

Upvotes: 2

Views: 136

Answers (2)

Darren
Darren

Reputation: 70796

The problem is you're modifying the attribute "checked" in the DOM but the same event handler is not being called.

In your button event click you could use trigger and do:

$('input[type="checkbox"]').trigger('change');   

http://jsfiddle.net/wmVtB/7/

Upvotes: 1

aefxx
aefxx

Reputation: 25279

No, setting the checked attribute via JavaScript does not fire any event. If you need to fire one, you would have to script it yourself or trigger a click event, which in turn does then fire the state change.

Upvotes: 1

Related Questions