0

I am working on the ordering process for a site and I am experimenting with having an order alert box displayed in the window at all times.

Currently I have this box set to show how many items are in your order.

I would like to display the name of the product and the qty in this box also.

Below is the code that controls this box:

$(document).ready(function(){
    //if cookie exists, show the panel
    if($.cookie('order_cookie') != undefined){
    productArray = JSON.parse($.cookie('order_cookie'));
    $(".order-alert").show();

    for(var i = 0; i < productArray.length; i++){
       console.log(productArray[i]);
       var obj = productArray[i];
       console.log("Object code: " + obj.stockCode + " Qty: " + obj.quantity);
    }

    $('#order_counter').html(productArray.length);
    }
});

The console.log is printing the correct data, I guess it's just a case of inserting the data into the html

The HTML for the box is:

<section class="order-alert">
                <a href="xxxx">Your Order</a>
                <p>You have <span id="order_counter">0</span> items in your order</p>
    </section>

I would like to display the contents of the order below the counter but I'm not sure how I would do that.

1
  • So, for each order you would like a list below to show the contents? If so you can use jQuerys append method. Commented Jun 12, 2014 at 15:02

2 Answers 2

1

Just add append to your code -

for(var i = 0; i < productArray.length; i++){
   console.log(productArray[i]);
   var obj = productArray[i];
   console.log("Object code: " + obj.stockCode + " Qty: " + obj.quantity);
   $('.order-alert').append('<p>Object code: ' + obj.stockCode + ' Qty: ' + obj.quantity + '</p>');
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's a great solution, thank you I don't know why I didn't think of doing something like that. I don't suppose you'd know how to update the list every time an item is added? Or is that for another question perhaps?
That is another question because there are likely different actions involved.
1

Simply use the append on the div you want to put them in.

$("#myDiv").append($("<p>Object code: "+obj.stockCode+" Qty: "+obj.quantity+"</p>"));

You can put any html string in the jquery fragment initializer. { $(" < html > ") }

5 Comments

jQuery Fragment Initializer?
What would you prefer me to call it? I've never seen it called anything other than this. (in stack traces)
It's the jQuery object. But it can be used by other libraries too.
Pardon me, It's a Dom fragment. As in, "Jquery's fragment initializer". So it's not wrong.
It's OK @JosephDailey - I just read it and laughed a little because it sounded like some sort of sci-fi weaponry.

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.