Abs
Abs

Reputation: 57916

Getting the same value of the radio button selected using Val()

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

Upvotes: 0

Views: 1322

Answers (1)

&#211;lafur Waage
&#211;lafur Waage

Reputation: 69991

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.

Upvotes: 3

Related Questions