<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang = "en" dir="ltr">
<head>
<title> </title>
<meta http-equiv="content-type"
content="text/html;charset=iso-8859-1" />
<script type="text/javascript">
var emptyCart = true;
var salesTotal = 0;
var curRow = 1;
function addItem (selectedItem) { if (emptyCart == true) {document.getElementById("shoppingCart").deleteRow(0); emptyCart = false; }}
var lastItem = document.getElementById("shoppingCart").rows.length;
var cartTable = document.getElementById("shoppingCart");
var newRow = cartTable.insertRow(lastItem);document.getElementById("chocolateTable").rows[lastItem].id = "R" + curRow;
var itemCell = newRow.insertCell(0); itemCell.innerHTML = selectedItem;
var priceCell = newRow.insertCell(1); priceCell.innerHTML = itemPrice;
var actionCell = newRow.insertCell(2); actionCell.innerHTML = "<input type='button' value='Remove' " + "onclick=\"removeItem('R" + curRow + "')\" />"; ++curRow;
var curItem = document.getElementById("chocolateTable").rows[selectedItem].cells;
var selectedItem = curItem[0].innerHTML;
var itemPrice = curItem[1].innerHTML;
salesTotal += parseFloat(itemPrice.substring(1));document.getElementById('total').innerHTML = "<strong>Sales total</strong>: $" + salesTotal.toFixed(2);
function removeItem(rowNum) {if (document.getElementById("shoppingCart").rows.length == 1) {
document.getElementById("shoppingCart").rows[0].cells[0].innerHTML = "<td>Your shopping cart is empty</td>";
document.getElementById("shoppingCart").rows[0].cells[1].innerHTML = "<td>$0.00</td>"; salesTotal = 0;
document.getElementById('total').innerHTML = "$" + salesTotal.toFixed(2);emptyCart = true;
}
else {
var selectedRow = document.getElementById(rowNum).rowIndex; document.getElementById("shoppingCart").deleteRow(selectedRow);
var itemPrice = document.getElementById("shoppingCart").rows[0].cells[1].innerHTML; salesTotal = salesTotal - parseFloat(itemPrice.substring(1));document.getElementById('total').innerHTML = "$" + salesTotal.toFixed(2);
}
}
</script>
</head>
<body>
<h1>Central Valley Chocolates</h1>
<h2>Gourmet Chocolates</h2>
<table border="1" id="chocolateTable">
<tr id="ch1">
<td>Chocolate Truffles</td><td>$34.99</td><td>
<input type="button" value="Add" onclick="addItem(document.getElementById('ch1').rowIndex)" /></td></tr>
<tr id="ch2">
<td>Pecan Caramel Duets</td><td>$14.99</td><td>
<input type="button" value="Add" onclick="addItem(document.getElementById('ch2').rowIndex)" /></td></tr>
<tr id="ch3">
<td>Chocolate Covered Cherries</td><td>$28.99</td><td>
<input type="button" value="Add" onclick="addItem(document.getElementById('ch3').rowIndex)" /></td></tr>
<tr id="ch4">
<td>White Chocolate Ganaches</td><td>$22.99</td><td>
<input type="button" value="Add" onclick="addItem(document.getElementById('ch4').rowIndex)" /></td></tr>
<tr id="ch5">
<td>Chocolate Mints</td><td>$17.99</td><td>
<input type="button" value="Add" onclick="addItem(document.getElementById('ch5').rowIndex)" /></td></tr>
<tr id="ch6">
<td>Chocolate Caramels</td><td>$14.99</td><td>
<input type="button" value="Add" onclick="addItem(document.getElementById('ch6').rowIndex)" /></td></tr>
<tr id="ch7">
<td>Chocolate Toffee Bark</td><td>$9.99</td><td>
<input type="button" value="Add" onclick="addItem(document.getElementById('ch7').rowIndex)" /></td></tr>
</table>
<h2>Your Shopping Cart</h2>
<table id="shoppingCart" border="1">
<tr><td>Your shopping cart is empty</td></tr>
</table>
<p id="total"> </p>
</body>
</html>
This post has been edited by Dormilich: 31 January 2012 - 01:05 AM
Reason for edit:: please use [CODE] [/CODE] tags when posting code

New Topic/Question
Reply


MultiQuote




|