views:

98

answers:

1

Given the following code, jQuery seems to only alert "1" as the value even after the user has selected radiobutton 2. What am I missing here? Shouldn't the trailing jQuery alert "2" when the second radiobutton is selected?

<p>
  Coal  <input type="radio" name="example" value="1" checked /> 
  Candy <input type="radio" name="example" value="2" />
</p>

I'm using this code in firebug to test:

r = $(":radio[name='example']").val();
alert(r);
+5  A: 

$(":radio[name='example']") gives you all radios with name=example .val() then takes the value of the first of those. you want $(":radio[name='example']:checked").val()

just somebody
Of course. I can't believe I overlooked that :)
Jonathan Sampson