Reputation: 6812
This code won't work:
$(document).on('change', '#myRadioButton', function(){ // Won't work
});
I want to catch whenever a radiobutton is deselected or selected, but the code above doesn't catch when it's unselected.
What's the proper way to catch radio button deselection?
Upvotes: 0
Views: 153
Reputation: 59262
Do this:
$('#myRadioButton').change(function(){
alert("changed");
});
$('input[type=radio]').not('#myRadioButton').change(function(){
$('#myRadioButton').change();
});
What it does ?
We know that, a radio button only gets deselected when any other radio button gets selected.
So I basically bind a change event for #myRadioButton
and then bind all other radio buttons except #myRadioButton
to the change event and on it, i invoke #myRadioButton
's change event. like $('#myRadioButton').change();
Here is the JsFiddle I'm damn sure, that it will work.
Mark it as answer if it helps :)
Upvotes: 0
Reputation: 74420
I guess, you should use something like that, setting onchange event on all radios from same group:
$(document).on('change', ':radio[name=mygrp]', function(){
alert($('#myRadioButton').is(':checked'));
});
Upvotes: 4