Reputation: 35
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
Reputation: 1765
Using vanilla js:
var preview = document.getElementById("itemPreview");
document.getElementsByTagName("LineShoppingCart").forEach(function (el) {
preview.innerHTML += el.innerHTML;
});
Upvotes: 0
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