4
I’m assigning the sum of a column in an HTML table to a JS (sum) variable, but when I print it on the screen I get an Nan, someone knows why?
<tr>
<td style="background:#c4ffd6;" align="center" class="qtd_recuperado">
<?php echo "$recuperado_total"; ?>
</td>
</tr>
Then, in JS, I add up:
<script type="text/javascript">
function sumQuantity() {
var elements = document.getElementsByClassName('qtd_recuperado');
var sum = 0;
for (i=0;i< elements.length;i++) {
sum = sum + parseFloat(elements[i].innerHTML);
};
document.getElementById('resultado').innerHTML = sum;
}
sumQuantity();
</script>
So I try to print, like this:
<td style="background:#c4ffd6;" align="center" class="qtd_recuperado" id="resultado"></td>
Can you put here the HTML that reaches the browser? (after PHP). And by the way, if you generate it in PHP and it’s static HTML because you don’t add it up in PHP either?
– Sergio
I managed to make the sum using JS, the problem is time to show, so I did not do in PHP.
– Gabriel Tobias
Okay, so the sum works as you want but insert inside
#resultado
no?– Sergio
Related: https://answall.com/a/211239/57801
– Don't Panic