I found here this script that does almost what I need (find out if one radio button within a div is checked), although, it alerts me for each radio button, I just need one alert if one of them is checked.
Background info: If one is checked I will show the next div. If not the div won't show. On top in my system, a click on a checked radio button will uncheck it, then the showed div will be hidden.
My code:
<div class="divtop">
<input type="radio" name="r1" value="v1" />
<input type="radio" name="r1" value="v2" />
<input type="radio" name="r1" value="v3" />
<div>
Here is the script:
$('.divtop input:radio').live('click', function(event) {
var div = $("div.divtop");
var radiobuttons = div.find("input[type='radio']");
for (var i = 0; i < radiobuttons.length; i++) {
if (radiobuttons[i].type === 'radio' && radiobuttons[i].checked) {
alert ('on') // action
} else {
alert ('off') // action
}} });
How can I adapt this script to get only one alert when one of the radio button is checked?
The function must be generic (no use of radio button names) because it will apply in different situations with different groups of radio buttons.