Because sharing is caring

Radio Button values in jQuery

Posted: August 27th, 2010 | Author: | Filed under: Javascript, JQuery, Web Development | Tags: , , , , | No Comments »

Getting the values of radio buttons in Javascript can be a bit of a pain in the behind at times.
You’ll have to assign a different ID to each radiobutton, loop over them to see which one is checked, and return that value.
Too much hassle.

However, jQuery selectors to the rescue!
Suppose you have the following radio buttons:

<input type="radio" name="animals" value="Dog" />
<input type="radio" name="animals" value="Cat" />
<input type="radio" name="animals" value="Bird" />

Getting the selected value could be done like so:

$('input[name=animals]:checked').val()

Try it here:

Dog

Cat

Bird

Good luck, hope it helps someone!



Leave a Reply