views:

49

answers:

3

Is there some event for a radio button being selected?

I know I can use "click" but I believe it is possible to style radio buttons in some browsers such that clicking an outter area of a radio button might not select the radio button.

What event should I use?

+2  A: 

You can use the onchange event, which will fire when the radio selection is changed (ie. the first time a radio button in the group is clicked or when the selection within the group is changed).

See http://jsfiddle.net/P9Z9Y/1/ for a simple example (clicking on either the label or the radio button itself will trigger an alert if the radio group's value changes).

Daniel Vandersluis
Note that this will in IE only get fired when the input loses focus. Clicking a button would only keep its focus. You'd like to click/tab somewhere else thereafter to let it to lose focus and thus get the `change` event to fire.
BalusC
@Balus that's not the case for radio buttons in Firefox or Chrome (onchange on a radio button fires immediately on a click that changes the radio group value, and even when clicking on an associated `<label>`, see my updated jsfiddle). I don't have a newer version of IE here to test with.
Daniel Vandersluis
Or IE (back to version 4), NN 4.x, Safari, Opera, or even the Lotus Notes client either. For keyboard-editable fields, yes, but not for radio buttons.
Stan Rogers
+1  A: 

did you try using the onchange event?

Aaron Saunders
+1  A: 

It depends when you want to be informed of the event.

If you want to know immediately, go with click. IE updates the state of checked before the handler function is called, and I think the other browsers do as well. You may want to double check as I only have IE to work with right now.

If you only need to know before something else happens, you can use change. IE will not fire the change event until the selected radio button loses focus. FF/chrome/others may fire the event without focus changing, but I believe IE is actually doing it right in this case.

lincolnk