4 Replies - 369 Views - Last Post: 01 February 2012 - 09:12 PM

Topic Sponsor:

#1 commochief  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-January 12

I am difficulties with DOM

Posted 31 January 2012 - 01:01 AM

I cant seem understand the problem. i have narrow it down to line 16-20 but i may be wrong. Here is my codes. Any assistance and guidance will be greatly appreciated..

<!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">&nbsp;</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


Is This A Good Question/Topic? 0
  • +

Replies To: I am difficulties with DOM

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 2146
  • View blog
  • Posts: 5,429
  • Joined: 08-June 10

Re: I am difficulties with DOM

Posted 31 January 2012 - 01:08 AM

the problem is that you try to access elements not yet existing. you would need to delay the JS execution until the DOM is ready (useful events: DOMContentLoaded, load).
Was This Post Helpful? 0
  • +
  • -

#3 commochief  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-January 12

Re: I am difficulties with DOM

Posted 31 January 2012 - 01:32 AM

Dormilich,

Thanks for the quick reply.. I am difficulites understanding DOM. You suggested that i should delay the JS execution untilthe DOM is ready, how should i do that?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 2146
  • View blog
  • Posts: 5,429
  • Joined: 08-June 10

Re: I am difficulties with DOM

Posted 31 January 2012 - 09:14 AM

you wait for an event that says "the DOM is loaded" which is (as mentioned) DOMContentLoaded or you wait for the event that says "all is loaded" (the load event)

This post has been edited by Dormilich: 31 January 2012 - 09:14 AM

Was This Post Helpful? 0
  • +
  • -

#5 commochief  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-January 12

Re: I am difficulties with DOM

Posted 01 February 2012 - 09:12 PM

Dormilich,

Thanks for the assistance. I was able to fix it. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1