I am having an issue summing the values of elements having a certain name. I have been attempting to use my own version of the answer from this post, but am having no luck.
Here is where the elements I want to sum are found:
@foreach ($items as $item)
<tr>
<td>{{ $item[0]->name }}</td>
<td>{{ $item->quantity[0]->quantity }}</td>
<td>${{ $item[0]->price }}</td>
<td name="subtotal">{{ $item->quantity[0]->quantity * $item[0]->price }}</td>
</tr>
@endforeach
This all outputs correctly and as you can see, I am using laravel 5's blade and I am wondering if this has to do with why I can't get this to work. If there is a better way to do this with laravel, please let me know.
Here is my script that I am using, it is placed below the above code:
<script>
var arr = document.getElementsByName('subtotal');
var tot=0;
for(var i=0;i<arr.length;i++){
if(parseFloat(arr[i].value))
{
tot += parseFloat(arr[i]);
}
}
document.write(tot);
</script>
I have been messing with it for a while and have not been able to have any luck. What I do know is that the length of the array ends up being 2 (which it should), but it never makes it inside the if statement.
When I check the output by using document.write(arr[0].value), it outputs 'undefined' and I can't seem to pin point as to why this is. The value of tot always ends up being 0.
Any help is appreciated and let me know if any more info is needed.
Thanks!