# Sum of several values

on
Good evening everyone !
I try to have the total sum of several value in js but I can't.
``````function Gift(a){
var CHK_Gift = document.getElementById("CHK_Gift_" + a); //value 1000 + value 700 + value 500

} else {
var POINT = 0;
}

}
``````
I don't already know if I'm doing it right, but that's the problem,
As you can see, I have several CHK_Gift and VAL_Gift values that I am trying to have a running total.
Only, but condition does not allow me.

When I click on:
and wishes, in PNT_Gift to have the total sum, that is 2200

Can you help me please ?
Thank you in advance and happy holidays!
Comment
Watch Question

Do more with

EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:
it's safest tp to compute all checkbox every time, not only trying to start from previous checked or not
test page : https://jsfiddle.net/835e06hy/

happy holidays to you

``````<input id="CHK_Gift_0" type="checkbox" value="1000"/>1000<br>
``````

``````window.addEventListener("DOMContentLoaded", function(event) {
for(var i=0;i<3;i++) {
}
});

var total = 0;
for(var i=0;i<3;i++) {
}
}
}
``````

Commented:
Thank leakim971,

Thanks again for your quick response.

In your example and your test page, I don't get errors, while in mine I have these two errors ...
Multitechnician
Top Expert 2014

Commented:
not matching IDs ?

Commented:
Hello Leakim,

I understood the origin of the problem.
It is the limit of i <3 that poses the problem.
I can't delete i <3, so what if my boxes are greater or less than say 5 or 7 or 10?

``````<?php
try {\$stat = \$pdo_jeux->query("SELECT * FROM tb_jeu_recap");
\$i = 0; while(\$data = \$stat->fetch(PDO::FETCH_ASSOC)){
?>

<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="CHK_Gift_<?php echo \$i; ?>" value="<?php echo \$data["Points"]; ?>">
<label class="custom-control-label" for="CHK_Gift_<?php echo \$i; ?>"><?php echo \$COUPON; ?></label>
</div>

<div class="input-group-prepend">
</div>

for(var i=0; i<3; i++) {
}
});

var total = 0;
for(var i=0; i<3; i++) {
total += document.getElementById("CHK_Gift_" + i).value * 1;
}
}
}
``````

Commented:
For now I have done this and it works, but I don't know if it's the correct formula ...

for(var i=0; i<"<?php echo \$i;?>"; i++) {
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
An alternative approach is to give each of your checkboxes a class, and then use that to select them all:

``````<input type="checkbox" class="gifts custom-control-input" id="CHK_Gift_<?php echo \$i; ?>" value="<?php echo \$data["Points"]; ?>">
``````
``````function Gift() {
let total = 0;

for( let i = 0; i < gifts.length; i++ ) {
}

}
``````
Multitechnician
Top Expert 2014

Commented:
replace :
for(var i=0;i<3;i++) {
by :
for(var i=0;i<max;i++) {

and somewhere :
``````<?php
echo 'var max = ' . \$i . ';';  // where \$i have the number of checkboxes
?>
``````

Commented:
Thank you for your precious help!
Multitechnician
Top Expert 2014

Commented:
you welcome