Jump to content

dynamically created shoppingCart table problem


thehindutimes
 Share

Recommended Posts

hey!I'm writing a little eCommerce site as an assignment for Uni and are having some difficulties with creating a dynamic table.It works fine if you only buy one thing, but as soon as you buy more than that it messes the cells up. The "products" are being stored in 3 arrays called "Name" "Sum" and "Quantity" and that's the information i want to present on the table.Does anyone know a good way to go around this?below is the JS I've got at the moment and I can't figure out how to get it to work properly.

var Name = new Array();var Quantity = new Array();var Sum = new Array();function ViewShoppingCart(){	//Getting the div	var CartCont = document.getElementById("CartContent");	CartCont.innerHTML = "";			//Creating variables	var CartTable = document.createElement("table");	var CartRow;	var NameCell;	var PriceCell;	         /*    for(var m = 0; m < Name.length; m++){				CartRow = document.createElement("tr");		CartTable.appendChild(CartRow);	        }*/	for(var i = 0; i < Name.length; i++ ){				CartRow = document.createElement("tr");		CartTable.appendChild(CartRow);				NameCell = document.createElement("td");				var text1 = document.createTextNode(Name[i]);		NameCell.appendChild(text1);		CartRow.appendChild(NameCell);						PriceCell = document.createElement("td");		QtyCell = document.createElement("td");		for(var x = 0; x < Sum.length && x < Quantity.length; x++ ){						var qty = document.createTextNode(Quantity[x]);			var text1 = document.createTextNode(Sum[x] * Quantity[x]);			total.push(text1);						PriceCell.appendChild(total[x]);			QtyCell.appendChild(qty);		}									CartRow.appendChild(PriceCell);			CartRow.appendChild(QtyCell);	}			//CartTable.border="1";		//CartTable.borderColor="Black";	CartCont.appendChild(CartTable);		}[/code block]Thanks for any help!
Link to comment
Share on other sites

You shouldn't be looping through the entire sum and quantity arrays for every entry in the name array. It sounds like you want to access Sum and Quantity if you're accessing Name, not another loop where you go through all of them for every product.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...