JavaScript javascript add checkbox-checked values javascript checkbox checked event,javascript checkbox checked value,javascript checkbox

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/

Answer:1

Oops! Typo!

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

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

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

Answer:2
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).

Answer:3

I'm learning Ajax and I'm confused about something. In a tutorial, these two lines are included document.myForm.time.value = ajaxRequest.responseText; //code <form name='myForm'> Name: <...

I'm learning Ajax and I'm confused about something. In a tutorial, these two lines are included document.myForm.time.value = ajaxRequest.responseText; //code <form name='myForm'> Name: <...

In Sharepoint, when we load the sp.js file after that when we use given code then it gives the current site context. But how can i get the site context using url. var context = new SP.ClientContext....

In Sharepoint, when we load the sp.js file after that when we use given code then it gives the current site context. But how can i get the site context using url. var context = new SP.ClientContext....

  1. clientcontext using c#
  2. using clientcontext sharepoint c#
  3. using clientcontext sharepoint
  4. using client context

I am working with PHP to display my log in form. Right now, the form shows "Username" but the text doesn't go away when the user starts to type. How can I have the text go away when the user starts to ...

I am working with PHP to display my log in form. Right now, the form shows "Username" but the text doesn't go away when the user starts to type. How can I have the text go away when the user starts to ...

  1. show text inside input field
  2. show text inside svg
  3. show text inside div
  4. show text inside password input
  5. show text inside css
  6. show text inside html
  7. tableau show text inside bar
  8. show image inside text box

I'm trying to pass all the values from some selected divs, where the ids of the divs are the values I want to get and aggregate to a textbox, but I'm not getting any result. Basically I want the id ...

I'm trying to pass all the values from some selected divs, where the ids of the divs are the values I want to get and aggregate to a textbox, but I'm not getting any result. Basically I want the id ...

  1. aggregate values in sql
  2. aggregate values in r
  3. aggregate values in excel
  4. aggregate values in tableau
  5. aggregate values in pivot table
  6. aggregate values in power bi
  7. aggregate values in pandas
  8. aggregate values in python
  9. aggregate values in power query
  10. aggregate values in teradata
  11. aggregate values in alteryx
  12. aggregate values in sql server
  13. oracle aggregate values from one column
  14. aggregate of values