Ali
Ali

Reputation: 267287

How to get the value of radio buttons via Jquery

If I have the following radios:

<input name="color" id="color_blue" value="blue">Blue
<input name="color" id="color_red" value="red">Red
<input name="color" id="color_green" value="blue">Green

What's the easiest way to find out which radio the user selected? Preferably something like to:

$("color").val();

Or something close to that.

Upvotes: 0

Views: 167

Answers (1)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196236

The answer is

$('input[name="color"]:checked').val()​​​​​​​​​​​​​​​;

but you have some errors in your html (in the question at least).. (no type="radio" specified..)

<input type="radio" name="color" id="color_blue" value="blue">Blue
<input type="radio" name="color" id="color_red" value="red">Red
<input type="radio" name="color" id="color_green" value="blue">Green

Upvotes: 4

Related Questions