1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Rep Power

    Some issues with javascript, forms and objects


    I'm trying to create a website with a shopping cart in JavaScript for a university assignment, but I'm having trouble with one of my functions, which instead of returning a number by itself, returns "0[object INPUT]", and I can't seem to figure out why!

    When the checkbox on the form in a different frame is checked, addToCart is called with the field name, which is supposed to change the quantity of the item in the ShoppingCart variable, and then print a message in one of the layers, saying whether the item has been added or removed. What I'd also like to do, using the viewTotal() function, is display how many items are in the shopping cart, and then (later on) the total price. However, in the layer that should be displaying the total number of items, I get the "0[object INPUT]" output, instead of a number by itself.

    These are the relevant sections of the JavaScript code:

    var ShoppingCart = new Object;
    function addItem(Description, Price, ItemNum) {
    	this.Description = Description;
    	this.Price = Price;
    	this.ItemNum = ItemNum;
    	this.display = printItem;
    	return this;
    function addToCart(field) {
    	form = field.form;
    	item_name = field.name;
    	quantity = form[item_name + "_quantity"];
    	if (field.checked) {
    		value = "Yes";
    	else {
    		value = "No";
    		quantity.value = 0;
    	Price = Fruits[item_name].Price;
    	ItemNum = Fruits[item_name].ItemNum
    	ShoppingCart[item_name] = new addItem(item_name, Price, ItemNum);
    	printItem(item_name, value);
    function printToLayer(layer, text) {
    	if (document.getElementById) {
    		x = document.getElementById(layer);
    		x.innerHTML = '';
    		x.innerHTML = text;
    	else if (document.all) {
    		x = document.all[layer];
    		x.innerHTML = text;
    	else if (document.layers) {
    		x = document.layers[layer];
    function viewTotal() {
    	var totalItems = 0;
    	with (top.trolley.document) {
    		for (items in ShoppingCart) {
    			totalItems = totalItems + ShoppingCart[items].ItemNum;
    		printToLayer("trolleyTotal", ["There are " + totalItems + " items in your trolley."]);
    Can anyone see any problems? I'm not asking anyone to write my code (please!), I'd just like some help on figuring out why this function isn't working.

    Thankyou very much!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Rep Power
    try debugging, cuz seriously..this code and it's intentions are really complicated. And not being able to see the whole code and the webpage with the form and everything working together makes it that much harder. Well for me anyway.

    This is what I suggest. Use alerts through out your code. Check your total value by alert(total);. basically send messages to yourself to see just where the problems start.

    Maybe this way you can narrow it down to a specific function.
    Post back with any results.
  4. #3
  5. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Rep Power
    neb...couple of points:

    This looks like you wanted the square brackets included in the string:

    printToLayer("trolleyTotal", ["There are " + totalItems + " items in your trolley."]);

    Leaving them outside (the quotes) makes the expression an array literal.

    Also: avoid the with statement like the plague. It does odd things to variable scope, and is inefficient.

    var d = top.trolley.document;
    for (items in d.ShoppingCart) {
    totalItems += d.ShoppingCart[items].ItemNum;

    ...or something like that. Best advice here: start at the very beginning of your script, and follow through its execution (with sample data) step-by-step, inserting breakpoints (alert()s) one by one to ensure the program flow is what you expected. Pseudocoding doesn't hurt, either. hth

IMN logo majestic logo threadwatch logo seochat tools logo