Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - Iterate through all checked radio buttons

I have a form which is similar like the one below:

<form id="myForm">
      Question 1:<br>
      <input type="radio" name="question1" value="Yes"> Yes
      <input type="radio" name="question1" value="No"> No
      <br>
      Question 2:<br>
      <input type="radio" name="question2" value="Yes"> Yes
      <input type="radio" name="question2" value="No"> No
      <br>
      Question 3:<br>
      <input type="radio" name="question3" value="Yes"> Yes
      <input type="radio" name="question3" value="No"> No
      <br>
      <input type="submit" value="Submit" name="submit">
</form>

I want to get all the selected radio button values from all the questions using jQuery and if all values is equal to "yes", it will alert success else it will alert fail.

This is the jQuery I wrote:

$(document).ready(function(){
   $("#myForm input[type=radio]:checked").each(function() {
       var value = $(this).val();
   });
});
like image 762
Nas Atchia Avatar asked Feb 09 '16 11:02

Nas Atchia


1 Answers

You can check if you ever get no with radio checked then result is fail else success.

Live Demo

result = "success";
$("#myForm input[type=radio]:checked").each(function() {
  if(this.value == "No" && this.checked == true)
  {
     result = "fail";
     return false;
  }
});
alert(result);  
like image 165
Adil Avatar answered Nov 06 '22 22:11

Adil