Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: how to check if all radio buttons in a div are selected

Tags:

my html looks like this

<div id="div1">   <input type="radio" name="r1" value="v1" />   <input type="radio" name="r1" value="v2" />   <input type="radio" name="r1" value="v3" />    <input type="radio" name="r2" value="v1" />   <input type="radio" name="r2" value="v2" />   <input type="radio" name="r2" value="v3" />    <input type="radio" name="r3" value="v1" />   <input type="radio" name="r3" value="v2" />   <input type="radio" name="r3" value="v3" /> </div> 

radio buttons are dynamically generated on my html so in that div i don't know how many radio buttons i have.

i want to make sure that the user will select a value for each one of them before he submits the form, how can i check that all radio buttons inside my div has a value checked?

Thank you

like image 441
trrrrrrm Avatar asked Aug 03 '11 01:08

trrrrrrm


People also ask

How do you check if all radio buttons in a div is selected JavaScript?

To find the selected radio button, you follow these steps: Select all radio buttons by using a DOM method such as querySelectorAll() method. Get the checked property of the radio button. If the checked property is true , the radio button is checked; otherwise, it is unchecked.

How can I know which radio button is selected via jQuery?

To check which radio button is selected in a form, we first get the desired input group with the type of input as an option and then the value of this selection can then be accessed by the val() method. This returns the name of the option that is currently selected.


1 Answers

$(":radio").change(function() {     var names = {};     $(':radio').each(function() {         names[$(this).attr('name')] = true;     });     var count = 0;     $.each(names, function() {          count++;     });     if ($(':radio:checked').length === count) {         alert("all answered");     } }).change(); 

Demo: http://jsfiddle.net/yFaAj/15/

like image 51
karim79 Avatar answered Sep 18 '22 19:09

karim79