1
With the help of @luccascosta I was able to make a code work in which, by clicking on a checkbox, it captures the value of this checkbox and updates the value in another input.
The problem now is that by clicking this checkbox, I need to update several inputs at the same time.
I even managed to do the calculations, but now I can not play the result in their respective inputs.
Look at the code:
var evento = document.querySelectorAll('input[type="checkbox"]');
for (let i = 0; i < evento.length; i++) {
evento[i].addEventListener('click', AddValor);
}
function AddValor() {
var val_evento = parseFloat(this.value, 10);
var pacote = new Array();
$('.valor_principal').each(function() {
pacote.push($(this).val());
});
for (let i = 0; i < pacote.length; i++) {
var total = jQuery.parseJSON(pacote[i]);
this.checked == true ? total += val_evento : total -= val_evento;
alert(total);
pacote[i].value = total;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="evento_value1" value="99.00">
<input type="checkbox" id="evento_value2" value="120.00">
<input type="checkbox" id="evento_value3" value="99.00">
<input type="checkbox" id="evento_value4" value="300.00">
<input type="checkbox" id="evento_value5" value="5.00">
<input class="valor_principal" value="100">
<input class="valor_principal" value="200">
<input class="valor_principal" value="300">
In Alert(total) you can see the result happening...
Aogra, I need him to update the values on each
Thanks for the help...
But you have 5 cehckbox only for 3 inputs, how it works?
– Miguel
That’s right... but that’s right...
– Rafael Schaffer Gimenes
Checkboxes are optional events and class="main value" inputs is the value of each package. When you click on a checkbox, you need to update the values of all class="main value_values".
– Rafael Schaffer Gimenes
I need the "main value" to be input, because the seller can still change the final value, taking your commission...
– Rafael Schaffer Gimenes
But shouldn’t five checkboxes for 5 inputs? What should happen to 2 checkboxes that are not covered by inputs? What each checkbox means in relation to each input?
– Miguel
Checkboxes and inputs class="main value_has no direct relation... Imagine the following. You went to a travel agency and they have 3 travel options (main value", but you have 5 optional events to include if you want.
– Rafael Schaffer Gimenes
What I want to do is, when I choose an optional event, add the value of that event to all the trips offered...
– Rafael Schaffer Gimenes
Ha ok, got it Rafael, I’ll help
– Miguel
Thank you @Miguel
– Rafael Schaffer Gimenes
Can it be with jQuery right? Since you’re importing it..
– Miguel
No problem, it can be yes...
– Rafael Schaffer Gimenes