Jun
10
2013

Sum values of all checked radio buttons

//Sum the values of selected radio buttons and put into input
$('body').on('change', 'input', function(){
    //Start the sum counter
    var thisCount = parseInt(0);
    //Run through all the selected radio buttons
    var data = $.map($("input:radio:checked"), function(elem, idx) {
          thisCount = + thisCount + parseInt( $(elem).val() );
          $('#totals').val(thisCount);
    });
});

//To Compile non integer data into a string
$('body').on('change', 'input', function(){
    //Start the output
    var thisValue = '';
    //Run through all the selected radio buttons and join with spaces
    var data = $.map($("input:radio:checked"), function(elem, idx) {
          return thisValue + $(elem).val();
    }).join(' ');
    $('#totals').val(data);
});

Leave a comment