I'm trying to compute the sum of many inputs that are displayed, in order to make an invoice. All products that must be invoiced are recorder in my database, and I wrote this JavaScript function to calculate the total:
<script type="text/javascript">
function getItems()
{
var items = new Array();
var itemCount = document.getElementsByClassName("items");
var total = 0;
for(var i = 0; i < itemCount.length; i++)
{
total = total + document.getElementById("p"+(i+1)).value;
}
return total;
document.getElementById('tot').value= total;
}
getItems()</script>
The problem is that I get Uncaught TypeError: Cannot read property 'value' of null on the line total = total + document.getElementById("p"+(i+1)).value;
I really do not understand why, because all my variables are declared.
document.getElementById("p"+(i+1))returns null, no element with id"p"+(i+1)was found.