Skip to content

Instantly share code, notes, and snippets.

@pankajpatel
Last active April 9, 2017 17:22
Show Gist options
  • Save pankajpatel/de287b98cbfecdf9db25fee2b3613dfa to your computer and use it in GitHub Desktop.
Save pankajpatel/de287b98cbfecdf9db25fee2b3613dfa to your computer and use it in GitHub Desktop.
<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>
<script>
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);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment