Search code examples
javascriptjquerysubtractioncalculated-field

How to subtract values of radio buttons


How can I calculate a set of radio buttons using JavaScript? It works as of now with adding up the total. but I would like it to Subtract the $150.00 off.

this is what I been working on now I get $Nan

<p>Baby Plan<br />
    [radio BPSUBPT id:BPSUBPT class:radio-vertical "Baby Plan $300.00 3 Sessions" "Baby Plan $500.00 4 Sessions"] </p>

<p>Did you have a Newborn session With ADP? <br />
[radio BPSUPQ id:BPSUPQ class:radio-vertical "Yes$ -150 off" "No"]


<p>Baby Plan Totals: <br />
Baby Plan Price: [text BPSUBPP 28/28 id:BPSUBPP]
Discount Amount: [text BPSUDA 8/8 id:BPSUDA]

Total Price: <span id="total"></span



<script>
        $(document).ready(function() {
      var inputs = $('input[name="BPSUBPT"], input[name="BPSUPQ"]');
            $(inputs).click(function() {
                var total = 0;
                $(inputs).filter(':checked').each(function() {
                    var value = ($(this).val()).match(/\$([0-9]*)/)[1];
                    total = (parseInt(total) - parseInt(value)) 
                })
               $('#total').html('$' + total);
            });
            $('input[name="BPSUBPT"]').click(function() {
                $(this).blur();
                $('#BPSUBPP').val($(this).val());
            })
            $('input[name="BPSUPQ"]').click(function() {
                $(this).blur();
                $('#BPSUDA').val($(this).val());
       });
        });
    </script>

Solution

  • You should probably just ADD the totals:

    <p>Baby Plan<br />
    [radio BPSUBPT id:BPSUBPT class:radio-vertical "Baby Plan $300.00 3 Sessions" "Baby Plan $500.00 4 Sessions"] </p>
    
    <p>Did you have a Newborn session With ADP? <br />
    [radio BPSUPQ id:BPSUPQ class:radio-vertical "Yes $-150 off" "No"]
    <!-- beware here because I changed "Yes$ -150" to "$-150" -->
    
    <p>Baby Plan Totals: <br />
    Baby Plan Price: [text BPSUBPP 28/28 id:BPSUBPP]
    Discount Amount: [text BPSUDA 8/8 id:BPSUDA]
    
    Total Price: <span id="total"></span
    

    jQuery

    $(document).ready(function() {
        var inputs = $('input[name="BPSUBPT"], input[name="BPSUPQ"]');
        $(inputs).click(function() {
            var total = 0;
            $(inputs).filter(':checked').each(function() {
                // Now including the - sign
                var value = ($(this).val()).match(/\$(-?[0-9]*)/)[1];
                if (value) {
                    // I'm now ADDing the total
                    // total = total + parseInt(value);
                    total += parseInt(value);
                }
            });
            $('#total').html('$' + total);
        });
        $('input[name="BPSUBPT"]').click(function() {
            $(this).blur();
            $('#BPSUBPP').val($(this).val());
        });
        $('input[name="BPSUPQ"]').click(function() {
            $(this).blur();
            $('#BPSUDA').val($(this).val());
        });
    });