watermelon_heart
watermelon_heart

Reputation: 35

How to write to div dynamically, can be multiple times

I am trying to write text to a div dynamically.

I have this piece of jquery:

            $.each($('.LineShoppingCart'), function () {

            $("#itemPreview").html($(this).html());


        });

and HTML:

<div id="itemOrderPreview"></div>

It works partially because it does not work when .LineShoppingCart has multiple values. It only writes the last value.

If I write the jquery with an alert, for example:

            $.each($('.LineShoppingCart'), function () {
            alert($(this).html());


        });

Then I can see the alert popping up multiple times one for each value.

What do I have to do differently when writing the value to a div?

Many thanks.

Upvotes: 0

Views: 117

Answers (2)

alzclarke
alzclarke

Reputation: 1765

Using vanilla js:

var preview = document.getElementById("itemPreview");
document.getElementsByTagName("LineShoppingCart").forEach(function (el) {
  preview.innerHTML += el.innerHTML;
});

http://jsfiddle.net/mspzLz81/

Upvotes: 0

Verhaeren
Verhaeren

Reputation: 1661

Use append:

$.each($('.LineShoppingCart'), function () {
    $("#itemPreview").append($(this).html());
});

To learn about append: http://api.jquery.com/append/

To learn about prepend: http://api.jquery.com/prepend/

Upvotes: 2

Related Questions