The problem is that you’ve declared sum
inside your function, but then this code hidden away at the bottom of your fiddle tries to use it **outside* the function (It has nothing to do with loops; in fact, I don’t see any loops in your code):
<script>document.getElementById("total").innerHTML=sum</script>
That fails because there is no global sum
variable.
You probably want to put that code in your function, rather than in a separate script block unrelated to it. Updated Fiddle That said, it’s unclear why you have both total1
and sum
, but my guess is that this code is only part-way complete and total1
will be (in the final version) for just one row, and sum
will be for all rows. Or something like that.
2
solved JavaScript variable from function used outside the function [duplicate]