0

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?

3
  • 1
    show your html please Commented Feb 6, 2014 at 15:18
  • 1
    in radio buttons there is no deselection operation... there in only a new selection which will deselect the previous one.. Commented Feb 6, 2014 at 15:18
  • @Weblurk, check out my answer too plz.. :) Commented Feb 6, 2014 at 15:39

2 Answers 2

4

I guess, you should use something like that, setting onchange event on all radios from same group:

DEMO jsFiddle

$(document).on('change', ':radio[name=mygrp]', function(){ alert($('#myRadioButton').is(':checked')); }); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Ended up using this solution. Any idea of why the 'change' event won't fire when radio is deselected? You ARE changing it's value after all.
Should see the specification but i think this is not what specification says, fire change event of radio button when deselected.
0

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 :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.