DeathKing
DeathKing

Reputation: 311

Getting how many checkboxes and radio buttons are checked

So my question is how will I be able to count the checkboxes that are checked and the radio buttons that have a "yes" for an answer... the code I am using right now for the radio button is:

<label class="radio-inline">
<input form="ES3S" type="radio" name="Textbook'.$i.'" value="'.$Result[$i]['ID'].'"> Yes
</label>
<label class="radio-inline">
<input form="ES3S" type="radio" name="Textbook'.$i.'" value="-1">No
</label>


<span class="d-inline-block" data-toggle="popover" title="Error" data-content="This book is required by the school. If you want to cancel this out, proceed to the principals office with the book for review." data-trigger="hover">
<input form="ES3S" required checked onclick="return false;" type="checkbox" value="'.$Result[$i]['ID'].'" name="Textbook'.$i.'">
</span>

I was thinking of having a timer that does a checks every second to check how many check boxes and radio buttons are checked but I suppose that is very impractical.

SetInterval(ScriptUpdate, 1000);


function ScriptUpdate(param){


var check = $('#form').find('input[type=checkbox]:checked').length;
   alert('You have checked forms');
   return false;
}):    }

Cheers! Thanks in advance

Upvotes: 1

Views: 668

Answers (4)

asha_ramoliya
asha_ramoliya

Reputation: 46

Filter based on "yes" text and checked radio button:

var radioButtonSelectedCount = $('input[type=radio]:checked').parent().filter(function() {
       return $(this).text().trim()=="Yes"
    }).length;

Upvotes: 1

Mark Baijens
Mark Baijens

Reputation: 13222

For radio buttons you can do something like this.

var countRadio = 0;
$('label.radio-inline').each(function(){
    if($(this).text() == "yes") {
        if($(this).children('input[type=radio]:checked').length > 0) {
            countRadio++;
        }
    }
});
console.log('Counted ' + countRadio  + ' Radiobuttons with yes');

Upvotes: 0

nandal
nandal

Reputation: 2634

Use the following:-

function checkboxes(){
    var checkBoxCheckedCount = document.querySelectorAll('input[type=checkbox]:checked').length;
    var radioButtonSelectedCount = document.querySelectorAll('input[type=radio]:checked').length;
    alert(checkBoxCheckedCount+radioButtonSelectedCount);

}

Then you can use the checkBoxCheckedCount and radioButtonSelectedCount Variables as per your needs.

Upvotes: 0

cnrhgn
cnrhgn

Reputation: 703

Multiple ways to count the number of checkboxes/radio buttons checked, this is probably the simplest:

$(document).ready(function(){
  alert($("input[type=radio]:checked").length);
});

Upvotes: 0

Related Questions