Sampson
Sampson

Reputation: 268414

jQuery Getting Wrong Value from Checkbox Group?

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);

Upvotes: 1

Views: 706

Answers (1)

just somebody
just somebody

Reputation: 19247

$(":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()

Upvotes: 9

Related Questions