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?
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).
To find the selected radio button, you follow these steps: Select all radio buttons by using a DOM method such as querySelectorAll() method. Get the checked property of the radio button. If the checked property is true , the radio button is checked; otherwise, it is unchecked.
By default, the control returns a Boolean numeric value to indicate its state (0 for deselected and 1 for selected). However, you can change the return value for a radio button control with the Value property.
To define the click event handler for a button, add the android:onClick attribute to the <RadioButton> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With