<p id="sum">0</p>
<div id="items">
<p><input type="checkbox" class="item item-1" value="5" />5</p>
<p><input type="checkbox" class="item item-2" value="14" />14</p>
<p><input type="checkbox" class="item item-3" value="3" />3</p>
<p><input type="checkbox" class="item item-4" value="8" />8</p>
</div>
items = Array.prototype.slice.call(document.querySelectorAll('.item'))
function add() {
let sum = 0
items.forEach(item => {
if(item.checked) {
sum += parseInt(item.value);
}
})
document.querySelector('#sum').innerHTML = sum;
}
items.forEach((item) => {
item.addEventListener('click', add);
});
View Compiled
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.