user2726041
user2726041

Reputation: 359

How can I check if all radio buttons are checked

I have a number of radio buttons with the option, 'yes' or 'no'.

Is there a simple way with jQuery to check if they all have 'yes' selected?

HTML is:

<input type="radio" id="yes1" name="group1" value="yes">Yes<br>
<input type="radio" name="group1" value="No">No<br>
<hr>
<input type="radio" id="yes2" name="group2" value="yes">Yes<br>
<input type="radio" name="group2" value="No">No<br>
<hr>
<input type="radio" id="yes3" name="group3" value="yes">Yes<br>
<input type="radio" name="group3" value="No">No<br>

I'm guessing it's something along the lines of

yes1 = $("#yes1").prop("checked", true);
yes2 = $("#yes2").prop("checked", true);
yes3 = $("#yes2").prop("checked", true);

if (yes1 & yes2 & yes3) {
   // do something ?
}

Upvotes: 0

Views: 1907

Answers (6)

Satain
Satain

Reputation: 9

<!DOCTYPE html>
<html>

<head>
  <title></title>

</head>

<body>
  <input type="radio" id="yes1" name="group1" value="yes">Yes
  <br>
  <input type="radio" name="group1" value="No">No
  <br>
  <hr>
  <input type="radio" id="yes2" name="group2" value="yes">Yes
  <br>
  <input type="radio" name="group2" value="No">No
  <br>
  <hr>
  <input type="radio" id="yes3" name="group3" value="yes">Yes
  <br>
  <input type="radio" name="group3" value="No">No
  <br>
  <input type="button" id="btn" value="Test" />

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
      $("#btn").click(function() {
        var length = 3;
        var isChecked = true;
        for (var i = 1; i <= length; i++) {
          isChecked = isChecked && ($("#yes" + i).is(":checked"));
        }
        if (isChecked)
          alert("All are checked");
        else
          alert("All are not checked");
      });
    });
  </script>

</body>

</html>

Upvotes: 0

dennisjsk
dennisjsk

Reputation: 401

<!DOCTYPE html>
<html>

<head>
  <title></title>

</head>

<body>
  <input type="radio" id="yes1" name="group1" value="yes">Yes
  <br>
  <input type="radio" name="group1" value="No">No
  <br>
  <hr>
  <input type="radio" id="yes2" name="group2" value="yes">Yes
  <br>
  <input type="radio" name="group2" value="No">No
  <br>
  <hr>
  <input type="radio" id="yes3" name="group3" value="yes">Yes
  <br>
  <input type="radio" name="group3" value="No">No
  <br>
  <input type="button" id="btn" value="Test" />

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
      $("#btn").click(function() {
        var length = 3;
        var isChecked = true;
        for (var i = 1; i <= length; i++) {
          isChecked = isChecked && ($("#yes" + i).is(":checked"));
        }
        if (isChecked)
          alert("All are checked");
        else
          alert("All are not checked");
      });
    });
  </script>

</body>

</html>

Upvotes: 1

nalinc
nalinc

Reputation: 7425

You may check the count of radio buttons with value != true. If the count is Zero, all radio buttons would be selected.

if(!$('input[type="radio"][value="yes"]').not(':checked').length){
//select all radio buttons with value = yes
}

Upvotes: 1

ozil
ozil

Reputation: 7117

<input type="radio" id="yes1" name="group1" value="yes" checked>Yes<br>
<input type="radio" name="group1" value="No">No<br>
<hr>
<input type="radio" id="yes2" name="group2" value="yes" checked>Yes<br>
<input type="radio" name="group2" value="No">No<br>
<hr>
<input type="radio" id="yes3" name="group3" value="yes" checked>Yes<br>
<input type="radio" name="group3" value="No">No<br>


var yes1 = $("#yes1").is(":checked")
    var yes2 = $("#yes2").is(":checked")
    var yes3 = $("#yes3").is(":checked")
    //$("#Myradio").is(":checked")

    if (yes1 & yes2 & yes3) {
        alert('sasa');
       //do something
    }

FIDDLE

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388436

One way is to check whether all the radios with value as yes is checked

if($('input[type="radio"][value="yes"]').not(':checked').length == 0){
    //all checked
}

Upvotes: 2

Milind Anantwar
Milind Anantwar

Reputation: 82251

You can rather compare the length of elements with ['value=yes] with elements with ['value=yes] and property :checked:

 if($('[value=yes]').length==$('[value=yes]:checked').length){
     //all yes elements are checked
 }

Upvotes: 5

Related Questions