samedi 7 avril 2018

summing up in js with 2 options checked

i have this code in html

<input type="checkbox" value="12" id="logo" checked onchange="calculate(this);">
<input type="checkbox" value="19" id="bc" checked onchange="calculate(this);">
<input type="checkbox" value="200" id="first" onchange="calculate(this);">
<input type="checkbox" value="250" id="second" onchange="calculate(this);">

<p id="total"></p>

and then the js

var total = 0;

function calculate(option) {
    if (option.checked) {
        total += Number(option.value);
    } else {
        total -= Number(option.value);
    }
    document.getElementById("total").innerHTML = total;
}

i need the first two checked checkboxes to sum up from start when page loads and after the behavior of js to get me to 0 or to total of checkboxes when checking respectively unchecking

Aucun commentaire:

Enregistrer un commentaire