Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the selected radio button value

$("#poll1p").append("<input type='radio' name='radio1' id='r1' /><label for='radio1'>"+x[0]+"</label><br>");
$("#poll1p").append("<input type='radio' name='radio1' id='r2' /><label for='radio1'>"+x[1]+"</label><br>");

the values x[0] and x[1] are array values. I am not able to get the selected radio button value by using

$("input:radio[name='radio1']:checked").val();

If i use the above,i just get "ON" as a return value... I need the value from

like image 917
CuriousCoder Avatar asked May 17 '26 03:05

CuriousCoder


1 Answers

You need to specify a value="..." attribute for your radio buttons if you want some value besides "on" returned.

Use the following, and you should get back 1 or 2 from .val():

<input type='radio' name='radio1' id='r1' value="1" />
<input type='radio' name='radio1' id='r2' value="2" />

Not related, but you also need to use for="r1" and for="r2" for your lables - ie, use an element's id as the value of the for attribute of its label.

like image 168
meagar Avatar answered May 19 '26 16:05

meagar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!