views:

71

answers:

1

Hello all,

I have a form and I wish to get the value of a radio button but it always gives me the same value ("floating") even though I select a different radio button and I run this function?!

HTML:

<input type="radio" id="license_floating" name="license" value="floating" class="realtime" />
<input type="radio" id="license_fixed" name="license" value="fixed" class="realtime" />

JS/JQuery:

alert($('form#quick input[name=license]').val());

The form is named quick, however, there are other forms on the page which have use the same IDs. But this shouldn't be a problem as I am referencing it correctly by referring to the form as well as the element ID.

Should be asking for the selected one somehow?

Really need help on this!

Thanks all

+3  A: 

You can get the selected radio button with :checked

alert($('form#quick input[name=license]:checked').val());

This can also work if you want

alert($('form#quick input.realtime:checked').val());

But you state that #quick is in other places as well, as Gumbo pointed out in the comments, ID's should be unique.

Ólafur Waage
How? Like this: $('form#quick input[name=license]:selected').val()
Abs
Sorry, it's :checked and I updated the answer.
Ólafur Waage
Awesome. Worked very well!
Abs