1

Not sure if this is the correct title for this, so if its not then I am sorry. However, what I am attempting to do is this. Have an array that stores, Product Name, Price and Quantity. For multiple products that are entered using drop down boxes in HTML. Currently I have this.

var cart = new Object();

function main(uValue)
{
    //Get the value from the drop down
    var value = uValue.value;
    //Add on to the page the new value
    printOnPage(value);
    //Add 
    cart = addItem(value);
    printOnPage(cart);

}

function addItem(input)
{
    //cart.push(input);
    //return cart;

    //look up id in assoc. array
    //cart.numInCart++;
    cart.Amount = input;
    return cart;
}
function removeItem()
{
    cart.splice(1,1);
    return cart;
}

function printOnPage(input)
{
    var para = document.createElement("p");
    var paymentDiv = document.getElementById("payment");
    paymentDiv.appendChild(para);
    var txt = document.createTextNode(input);
    para.appendChild(txt);
}

And then this is the HTML that would relate to this.

<div class="item">
                <p>Faster than light car</p>
                <img src="i/toycar.jpg" width="85%" height="85%" alt="ToyCar" title="#WickCar">
                <p>Cost: £10 per item</p>

                <form name="form1" method="POST">
                    <select name="values" onchange="main(this)" >
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                        <option value="6">6</option>
                        <option value="7">7</option>
                        <option value="8">8</option>
                        <option value="9">9</option>
                    </select>
                </form>
            </div>

Ok, so a few questions. 1) am I going about this the correct way? 2) Why when I wish to view the content of the array do I get [object Object] enter image description here

Is there a better way to do this?

Thanks, hope someone can enlighten me to this.

9
  • 1
    Objects are not the same as arrays. Do you want the cart to be indexed by strings or by numbers? Commented Apr 29, 2012 at 14:56
  • @MДΓΓБДLL Can you not have an array of Objects? Or am I completely off with that? I don't mind, what ever would make it easier to get the details I require. So, I guess numbers then? Thanks Commented Apr 29, 2012 at 14:59
  • 1
    @Kyle93 yes you can have an array of objects, but they still have a numeric index. Unless you use an associative array, which acts more like an object anyhow Commented Apr 29, 2012 at 15:00
  • 1
    @vol7ron in JS, "object" and "associative array" are synonymous. @Kyle93 as vol7ron said, yes you can have an array of objects. However, that is not what var cart = new Object() gets you. Commented Apr 29, 2012 at 15:06
  • @MДΓΓБДLL maybe synonymous, but not the same. Associative arrays still have all the methods that come with the array constructor, which objects don't natively get. Commented Apr 29, 2012 at 15:08

1 Answer 1

2

Change printOnPage(cart); to printOnPage(cart.Amount);

It looks like you're trying to output cart, which is an object, rather than one of the values for a key in cart.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, thanks a lot. That sorted out the printing the value I wanted! I keep forgetting stuff like this in Java as well :( Lastly, do you have an idea how I could go about sending to the JS method the name and the cost as well? Would I use DOM or is there a simpler way? Thanks again!
There are many ways to go about this, but I feel you're reinventing the wheel - there are many decent shopping cart templates online already. Also, this is only the front end and any user can make their own front end to your site if they wish to, thus the backend is ever more important. To answer your question, for starters you should look at changing your cart to be a three dimensional array, or an array of objects (items). You will have to adjust your printOnPage to handle such a change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.