Skip to content
Advertisement

How to detect radio button deselect event?

Is there an easy way to attach a “deselect” event on a radio button? It seems that the change event only fires when the button is selected.

HTML

JavaScript

JavaScript

JavaScript

jsFiddle

Advertisement

Answer

Why don’t you simply create a custom event like, lets say, deselect and let it trigger on all the members of the clicked radio group except the element itself that was clicked? Its way easier to make use of the event handling API that jQuery provides that way.

HTML

JavaScript

jQuery

JavaScript

​Deselection events will trigger only among members of the same radio group (elements that have the same name attribute).

jsFiddle solution

EDIT: In order to account for all possible placements of the attached label tag (wrapping the radio element or being attached through an id selector) it is perhaps better to use onchange event to trigger the handlers. Thanks to Faust for pointing that out.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement