Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting Checked Radio Buttons with jQuery

Tags:

jquery

I have pages with varying numbers of radio buttons. These buttons have two different classes, yes, and no. As the user goes down the page, I need to figure out how many of each class they have selected, and display it in a <p> at the bottom of the page.

I'm having problems preventing 1 getting added to the variable when a user clicks on a radio button more than once. Here's a JS fiddle with my code:

http://jsfiddle.net/AnWU3/3/

It's not working at all for some reason, and I don't see why.

like image 504
JacobTheDev Avatar asked Dec 31 '25 01:12

JacobTheDev


1 Answers

Try the following:

$(document).ready(function() {
    $('input:radio').change(function(){
        var yes = $('.yes:checked').length
        var no = $('.no:checked').length
        $('.yes_results').text(yes)
        $('.no_results').text(no)                        
    })
});

DEMO

like image 122
undefined Avatar answered Jan 02 '26 17:01

undefined