Ahmed Adm
Ahmed Adm

Reputation: 31

Get values of all checked radio buttons using JQuery

Using JQuery I am trying to perform validation and also get the values of all the dynamically generated Radio buttons in the page.

I have 10 questions on my page and each question has a Radio button group(YES/NO).

when click in the radio button i want to send it's value to database for the question, this is my code

<p>Question 1</p>
<input id="1_1" type="radio" name="1" value="Yes" />
<input id="1_2" type="radio" name="1" value="NO" />

i searched in google and found this code

$('input[name=radioName]:checked', '#myForm').val()

But I don't know what is the right way to use it ?

Upvotes: 3

Views: 2410

Answers (2)

Amit Joki
Amit Joki

Reputation: 59232

If you want the values of all <radio> you can do

var ans = $('[name=1]:checked').map(funciton(){
    return $(this).val();
}).get();

Upvotes: 0

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18873

As per your HTML structure try this :-

var arr = []; // take an array to store values
$('input[type="radio"][name="1"]:checked').each(function(){
   arr.push($(this).val());  //push values in array
});

Upvotes: 4

Related Questions