I need a JavaScript that when clicking on a checkbox sum determined to a total already existing in another input. And when you disable the checkbox, subtract the checkbox value from this total.
I have the following:
<input type="checkbox" id="evento_value" value="99.00" onclick="AddValor()">
<input type="checkbox" id="evento_value" value="120.00" onclick="AddValor()">
<input type="checkbox" id="evento_value" value="99.00" onclick="AddValor()">
<input type="checkbox" id="evento_value" value="300.00" onclick="AddValor()">
<input type="checkbox" id="evento_value" value="5.00" onclick="AddValor()">
And also I have an input with an already established value, where you must add the values of click on each checkbox. If the user eventually disables the checkbox, subtract the value. I'm issuing the following code for this:
<script>
function AddValor() {
var resultado = parseInt(document.getElementById('evento_value').value, 10);
var total = parseInt(document.getElementById('valor_principal').value, 10);
if(document.getElementById('evento_value').checked == true) {
total = total +resultado;
} else if(document.getElementById('evento_value').checked == false) {
total = total - resultado;
}
$("#valor_principal").val(total);
}
AddValor();
Where #main_value is the input with the already calculated value in which you must add or subtract a certain value by clicking on the checkbox.
The fact is that this script only works for the first input checkbox and not for the others. I suspect it is related to IDs. Would anyone have any suggestions?