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
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.
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.
$(":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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With