2

I have a checkbox section that users can select to add features. I need each input's value to add to a sum to be presented in the #payment-total and #payment-rebill section. Essentially, if they select two of the checkboxes, #payment-total and #payment-rebill would = 100. I am able to get the first number to add/change, but having trouble getting #payment-rebill to change as well.

Here is my html:

<section id="extra-features">
<label class="header">Extra Features ($50/month): </label><br><br>
                    <div class="span3">
                    <label class="checkbox" for="Checkbox1">
                        <input value="50" type="checkbox" id="Checkbox1" value="option1" data-toggle="checkbox"> Instagram
                    </label>
                    <label class="checkbox">
                        <input value="50"  type="checkbox" id="Checkbox2" value="option2" data-toggle="checkbox"> Review site monitoring
                    </label>
                    <label class="checkbox">
                        <input value="50"  type="checkbox" id="Checkbox3" value="option3" data-toggle="checkbox"> Google+
                    </label>
                    <label class="checkbox">
                        <input value="50"  type="checkbox" id="Checkbox4" value="option4" data-toggle="checkbox"> LinkedIn
                    </label>
                    </div>
                    <div class="span3">
                    <label class="checkbox">
                        <input value="50"  type="checkbox" id="Checkbox5" value="option5" data-toggle="checkbox"> Pinterest
                    </label>
                    <label class="checkbox">
                        <input value="50"  type="checkbox" id="Checkbox6" value="option6" data-toggle="checkbox"> FourSquare
                    </label>
                    <label class="checkbox">
                        <input value="50"  type="checkbox" id="Checkbox7" value="option7" data-toggle="checkbox"> Tumblr
                    </label>
                    <label class="checkbox">
                        <input value="50"  type="checkbox" id="Checkbox8" value="option8" data-toggle="checkbox"> Advertising
                    </label>
                    </div>
                </section>

<div class="card-charge-info">
                Your card will be charged $<span id="payment-total">0</span> now, and your subscription will bill $<span id="payment-rebill">0</span> every month thereafter. You can cancel or change plans anytime.
            </div>

My javascript:

var inputs = document.getElementsByClassName('sum'),
    total  = document.getElementById('payment-total');
    total1 = document.getElementById('payment-billed');

 for (var i=0; i < inputs.length; i++) {
    inputs[i].onchange = function() {
        var add = this.value * (this.checked ? 1 : -1);
        total.innerHTML = parseFloat(total.innerHTML) + add
        total1.innerHTML = parseFloat(total1.innerHTML) + add
    }
}

Here is my jsfiddle: http://jsfiddle.net/rynslmns/6NJ8e/10/

Ryan Salmons
  • 1,429
  • 10
  • 21
  • 42

3 Answers3

4

Oops! Typo!

Your second span is called payment-rebill not payment-billed. This works

    total1 = document.getElementById('payment-rebill');

http://jsfiddle.net/6NJ8e/13/

hassassin
  • 5,024
  • 1
  • 29
  • 38
  • the only problem is that whenever the user unchecks the box and rechecks, it starts going into negative numbers. I added a link to the site http://www.thesomii.com/getstarted/prime/ – Ryan Salmons Jan 29 '14 at 21:50
  • Hmm, can you get a fiddle that has that issue? You might accidentally be changing the value of the checkbox to -50 or something. – hassassin Jan 29 '14 at 22:01
  • http://jsfiddle.net/rynslmns/uZbee/ first pick a billing term then check all the options in extra features, then select choose billing term from the select box, then start unchecking the checkboxes. you start getting negative numbers. – Ryan Salmons Jan 29 '14 at 22:11
  • Oh yes, the issue is that you are not maintaining any state when you pick a drop down. You can either calculate it on every change like the other answer or just keep track of the two values: http://jsfiddle.net/uZbee/2/ – hassassin Jan 29 '14 at 22:59
1
function updateTotals() {
    var inputs = document.getElementById('extra-features').getElementsByTagName('input');

    var sum = 0;
    for (var i = 0, num = inputs.length; i < num; i++) {
        if (inputs[i].checked) {
            sum += parseInt(inputs[i].getAttribute('value'));
        }
    }

    document.getElementById('payment-total').innerHTML = sum;
    document.getElementById('payment-rebill').innerHTML = sum;
}

var section = document.getElementById('extra-features');
var inputs = section.getElementsByTagName('input');

for (var i = 0, num = inputs.length; i < num; i++) {
    inputs[i].addEventListener('change', updateTotals);
}

JSFiddle: http://jsfiddle.net/6NJ8e/11/

This could be written a bit more succinctly with jQuery (or similar) since you have some DOM handling, but it isn't too bad without.

Basically, you have a function which sums everything and updates your fields.

To do this, you need to get a hold of all inputs. I used "extra-features" as the anchor since it was the closest ID-ed element.

Then, loop through them. If they are checked, add them to a total then just update your values.

If you used something that can do CSS-like selectors, you can usually just get the checked inputs directly (like $('#extra-features input:checked')) and skip the conditional.

Then, you just need to add a change event to each input which calls the updateTotals function any time something changes.

A bit of further optimization (depending on your exact usage), is you could store the inputs for further use so you don't have to look them up each time (but don't do this if you're in the global scope, only if this function would be encapsulated in some way).

samanime
  • 25,408
  • 15
  • 90
  • 139
  • the only problem is that whenever the user unchecks the box and rechecks, it starts going into negative numbers. thesomii.com/getstarted/prime – Ryan Salmons Jan 29 '14 at 21:53
  • Hi Ryan. I'm not quite sure what's going on in your specific implementation, but I couldn't reproduce the behavior in the JSFiddle version. Double-check that you don't have some other value contributing to a negative. In the version I wrote, it starts from zero and only adds from there. – samanime Jan 29 '14 at 21:59
  • http://jsfiddle.net/rynslmns/uZbee/ first pick a billing term then check all the options in extra features, then select choose billing term from the select box, then start unchecking the checkboxes. you start getting negative numbers. – Ryan Salmons Jan 29 '14 at 22:12
  • It's because you are using that weird way to decide your adding. If you want to continue to use that method, it should be 0, not -1. – samanime Jan 29 '14 at 22:22
  • Only because I couldn't get yours to work outside of the JSFIDDLE. when i added your snippet to my code, i wasn't getting any functionality. Here it is on my site with your code: http://www.thesomii.com/getstarted/basic/ – Ryan Salmons Jan 29 '14 at 22:32
  • I really don't mean to be a jerk, but I'm not going to debug your site for you. In the JSFiddle, if you change the -1 to 0 it will fix the negative problem. You then need to resum all of the values, so you should move it out of just the listener in to a separate function which you can call to get the sum on-demand, and then add that to the price instead of trying to track it. – samanime Jan 29 '14 at 22:36
1
total1 = document.getElementById('payment-rebill');

Gets the id 'payment-billed' and assigns a copy of it to var total1. Then

total1.innerHTML = parseFloat(total1.innerHTML) + add

Only changes the innerHTML of the COPY that you made a second ago. Try jQuery:

total1 = parseFloat(total1.innerHTML) + add
$("#payment-rebill").innerHTML(total1);

This way you do the calculations on the copy, then replace the original innerHTML with the copy's innerHTML.

EDIT -- To make it more clear, your original code was only changing the local copy total1. There wasn't any code to update the original #payment-rebill element.

Vladimir Rosca
  • 377
  • 1
  • 2
  • 15