First let’s add a tag to show the result:
<strong id="total">0</strong>
Now let’s go to javascript:
var checkboxes = document.querySelector('#opc1')
.querySelectorAll('input[type=checkbox]');
var total = document.querySelector('#total');
Object.keys(checkboxes).map((key) => {
checkboxes[key].addEventListener('change', function() {
total.innerHTML = +total.innerHTML + (this.checked ? +this.value : -this.value);
});
});
var checkboxes = document.document.querySelector('#opc1').querySelectorAll('input[type=checkbox]');
-> Take all the checkbox it contains in the div with ID "opc1";
var total = document.querySelector('#total')
-> picks up the tag with ID "total";
Object.keys(checkboxes).map((key) =>
-> How was a JSON returned in checkboxes, this line transforms the JSON keys into a list;
checkboxes[key].addEventListener('change', function() {
-> here he is listening (Listener) if each checkbox (checkboxes[key]) will have a change (change);
total.innerHTML = +total.innerHTML + (this.checked ? +this.value : -this.value)
-> If the checkbox is selected, it will add the previous value to the new one, otherwise it will decrease.
I hope I’ve helped, good studies!
var checkboxes = document.getElementById('opc1')
.querySelectorAll('input[type=checkbox]');
var total = document.querySelector('#total');
Object.keys(checkboxes).map((key) => {
checkboxes[key].addEventListener('change', function() {
total.innerHTML = +total.innerHTML + (this.checked ? +this.value : -this.value);
});
});
<div id="opc1" class="form-group" style="">
<label class="d-inline">
<input id="1" name="opc1" type="checkbox" value="100">
<span>100 personalizados</span>
</label><br>
<label class="d-inline">
<input id="2" name="opc2" type="checkbox" value="10">
<span>20 Centros de mesa com 2 Balões a gás em ca</span>
</label><br>
<label class="d-inline">
<input id="3" name="opc3" type="checkbox" value="20">
<span>Enchimento e orçamentação de 3000 balões</span>
</label><br>
<label class="d-inline">
<input id="4" name="opc4" type="checkbox" value="30">
<span>Bolo cenográfico de 3 andares</span>
</label><br>
<label class="d-inline">
<input id="5" name="opc5" type="checkbox" value="40">
<span>Cobertura Fotográfica</span>
</label><br>
<strong id="total">0</strong>
edited again
– Schielke
What would that be
value="1-100.00"
in the first checkbox? I have the answer ready, just do not publish because of this value.– Augusto Vasques
would be 100, already edited, sorry for the mistake!
– Schielke