Reputation: 49
I have searched high and low for a solution. Here is what I am trying to accomplish: I have a school project that is a webpage that has a shopping cart. The shopping cart needs to have a running total based on the user inputting numbers for each item. The user needs to be able to put a number in the quantity, and then change it and have the total price update dynamically. Currently we are only using javascript and HTML to make this happen (no AJAX or anything else yet). The result I am getting is only a 0 showing up in the total area. Here is the HTML
<div class="column col4"><h3>Quantity</h3>
<ul class="products">
<li><input oninput="subTotal('price1','quantity1', 'sub1'); runningTotal()" id="quantity1" name="quantity" placeholder="0" size="3"></li>
<li><input oninput="subTotal('price2','quantity2', 'sub2'); runningTotal()" id="quantity2" name="quantity" placeholder="0" size="3"></li>
<li><input oninput="subTotal('price3','quantity3', 'sub3'); runningTotal()" id="quantity3" name="quantity" placeholder="0" size="3"></li>
<li><input oninput="subTotal('price4','quantity4', 'sub4'); runningTotal()" id="quantity4" name="quantity" placeholder="0" size="3"></li>
<li><input oninput="subTotal('price5','quantity5', 'sub5'); runningTotal()" id="quantity5" name="quantity" placeholder="0" size="3"></li>
<li><input oninput="subTotal('price6','quantity6', 'sub6'); runningTotal()" id="quantity6" name="quantity" placeholder="0" size="3"></li>
<li><input oninput="subTotal('price7','quantity7', 'sub7'); runningTotal()" id="quantity7" name="quantity" placeholder="0" size="3"></li>
<li><input oninput="subTotal('price8','quantity8', 'sub8'); runningTotal()" id="quantity8" name="quantity" placeholder="0" size="3"></li>
Disregard the "subTotal" function as it functions perfectly fine. It is the "runningTotal" function that I am having trouble with. Here is all the price HTML
<div class="column col3"><h3>Price</h3>
<ul class="products">
<li id="price1" name="price" value="1.00">$1.00</li>
<li id="price2" name="price" value="2.00">$2.00</li>
<li id="price3" name="price" value="3.00">$3.00</li>
<li id="price4" name="price" value="4.00">$4.00</li>
<li id="price5" name="price" value="5.00">$5.00</li>
<li id="price6" name="price" value="6.00">$6.00</li>
<li id="price7" name="price" value="7.00">$7.00</li>
<li id="price8" name="price" value="8.00">$8.00</li>
</ul>
</div>
Here is the Javascript
function runningTotal() {
var quantity = document.getElementsByName("quantity");
var price = document.getElementsByName("price");
var total = 0;
for (var i = 0; i < 8; i++) {
total += parseInt(quantity[i].value,10) * parseInt(price[i].value, 10);
console.log(total);
}
document.getElementById("runtotal").innerHTML = total;
}
The idea of the function is to loop through the quantity and the price and multiply them together to update the total. This will be on input() so every time a number changes the function should run. However all that happens is a 0 shows up in the total and never changes. I understand that this is a pretty terrible way of accomplishing my end goal but its how we are instructed to do it currently. Any and all help would be GREATLY appreciated!
Upvotes: 1
Views: 2217
Reputation: 2755
You are getting invalid results because the input fields with name quantity
doesn't have a value
. Since there is no value
, the function parseInt(quantity[i].value, 10)
will give NaN
as result and all further calculations will result in errors.
Replace the function call parseInt(quantity[i].value, 10)
with parseInt(quantity[i].value || 0, 10)
to solve the issue. This will use 0
as the quantity if quantity[i].value
is undefined
.
See the code below.
function runningTotal() {
var quantity = document.getElementsByName("quantity");
var price = document.getElementsByName("price");
var total = 0;
for (var i = 0; i < 8; i++) {
total += parseInt(quantity[i].value || 0, 10) * parseInt(price[i].value || 0, 10);
}
document.getElementById("runtotal").innerHTML = total;
}
function subTotal() {}
<div class="column col4">
<h3>Quantity</h3>
<ul class="products">
<li><input oninput="subTotal('price1','quantity1', 'sub1'); runningTotal()" id="quantity1" name="quantity" placeholder="0" size="3"></li>
<li><input oninput="subTotal('price2','quantity2', 'sub2'); runningTotal()" id="quantity2" name="quantity" placeholder="0" size="3"></li>
<li><input oninput="subTotal('price3','quantity3', 'sub3'); runningTotal()" id="quantity3" name="quantity" placeholder="0" size="3"></li>
<li><input oninput="subTotal('price4','quantity4', 'sub4'); runningTotal()" id="quantity4" name="quantity" placeholder="0" size="3"></li>
<li><input oninput="subTotal('price5','quantity5', 'sub5'); runningTotal()" id="quantity5" name="quantity" placeholder="0" size="3"></li>
<li><input oninput="subTotal('price6','quantity6', 'sub6'); runningTotal()" id="quantity6" name="quantity" placeholder="0" size="3"></li>
<li><input oninput="subTotal('price7','quantity7', 'sub7'); runningTotal()" id="quantity7" name="quantity" placeholder="0" size="3"></li>
<li><input oninput="subTotal('price8','quantity8', 'sub8'); runningTotal()" id="quantity8" name="quantity" placeholder="0" size="3"></li>
</ul>
</div>
<div class="column col3">
<h3>Price</h3>
<ul class="products">
<li id="price1" name="price" value="1.00">$1.00</li>
<li id="price2" name="price" value="2.00">$2.00</li>
<li id="price3" name="price" value="3.00">$3.00</li>
<li id="price4" name="price" value="4.00">$4.00</li>
<li id="price5" name="price" value="5.00">$5.00</li>
<li id="price6" name="price" value="6.00">$6.00</li>
<li id="price7" name="price" value="7.00">$7.00</li>
<li id="price8" name="price" value="8.00">$8.00</li>
</ul>
</div>
<div id="runtotal"></div>
Upvotes: 2