simple cart
simple cart example
HTML
<div class="product1">
<div class="productName">
Product1
</div>
Price
<lable class="productPrice" id="price1">10</lable>
<br /> qty
<input type="text" class="txtQty" id="qty1">
<button class="Buy" id="button1">
add to cart
</button>
</div>
<div class="product1">
<div class="productName">
Product2
</div>
Price
<lable class="productPrice" id="price2">20</lable>
<br /> qty
<input type="text" class="txtQty" id="qty2">
<button class="Buy" id="button2">
add to cart
</button>
</div>
<div class="product1">
<div class="productName">
Product3
</div>
Price
<lable class="productPrice" id="price3">30</lable>
<br /> qty
<input type="text" class="txtQty" id="qty3" >
<button class="Buy" id="button3">
add to cart
</button>
</div>
Total Qty
<lable id="totalQty">
</lable>
<br/> Total Price
<lable id="totalPrice">
</lable>
JavaScript
$("document").ready(function() {
var total=0;
var totalQty= 0;
$("#button1").click(function() {
var a = $("#price1").text();
var b = $("#qty1").val();
AddToCart(a,b);
});
$("#button2").click(function() {
var a = $("#price2").text();
var b = $("#qty2").val();
AddToCart(a,b);
});
$("#button3").click(function() {
var a = $("#price3").text();
var b = $("#qty3").val();
AddToCart(a,b);
});
function AddToCart(price,qty)
{
var sum = price*qty;
total = total + sum;
totalQty = totalQty + qty;
$("#totalPrice").text(total);
$("#totalQty").text(totalQty);
}
});