Clone Element and Append to end
I have a HTML template which looks like this:
<div id="amznplist">
<div id="amznpitem">
<span>
<img src="%%Produktbild%%" alt="%%Produkttitel%%"><div id="amzntitle">%%Produkttitel%%</div><div id="amzprice">%%Produktpreis%%</div>
</span>
</div>
</div>
What I try to archive is to load the entire html, copy the #amznpitem item, run through my response (data) and populate each li element with the data and add it to the end. What happens is that only one element is added to #amznplist. This is my current code.
var li = html.find('#amznpitem').clone();
html.find('#amznpitem').remove();
var ul = html.find('#amznplist');
$.each(data, function (index, values) {
li.find('#amzntitle').text(values['title'][0]);
li.find('a').attr('href', values['url'][0]);
li.find('img').attr('src', values['img'][0]);
li.find('img').attr('alt', values['title'][0]);
li.find('#amzprice').text(values['lowest_price'][0]);
$(ul).append(li);
});
$('#result').html(html);
javascript jquery
add a comment |
I have a HTML template which looks like this:
<div id="amznplist">
<div id="amznpitem">
<span>
<img src="%%Produktbild%%" alt="%%Produkttitel%%"><div id="amzntitle">%%Produkttitel%%</div><div id="amzprice">%%Produktpreis%%</div>
</span>
</div>
</div>
What I try to archive is to load the entire html, copy the #amznpitem item, run through my response (data) and populate each li element with the data and add it to the end. What happens is that only one element is added to #amznplist. This is my current code.
var li = html.find('#amznpitem').clone();
html.find('#amznpitem').remove();
var ul = html.find('#amznplist');
$.each(data, function (index, values) {
li.find('#amzntitle').text(values['title'][0]);
li.find('a').attr('href', values['url'][0]);
li.find('img').attr('src', values['img'][0]);
li.find('img').attr('alt', values['title'][0]);
li.find('#amzprice').text(values['lowest_price'][0]);
$(ul).append(li);
});
$('#result').html(html);
javascript jquery
use classes instead of ids - ids are meant to be unique and as such jquery will only fetch the first element with that id it finds
– Pete
Nov 21 '18 at 17:17
add a comment |
I have a HTML template which looks like this:
<div id="amznplist">
<div id="amznpitem">
<span>
<img src="%%Produktbild%%" alt="%%Produkttitel%%"><div id="amzntitle">%%Produkttitel%%</div><div id="amzprice">%%Produktpreis%%</div>
</span>
</div>
</div>
What I try to archive is to load the entire html, copy the #amznpitem item, run through my response (data) and populate each li element with the data and add it to the end. What happens is that only one element is added to #amznplist. This is my current code.
var li = html.find('#amznpitem').clone();
html.find('#amznpitem').remove();
var ul = html.find('#amznplist');
$.each(data, function (index, values) {
li.find('#amzntitle').text(values['title'][0]);
li.find('a').attr('href', values['url'][0]);
li.find('img').attr('src', values['img'][0]);
li.find('img').attr('alt', values['title'][0]);
li.find('#amzprice').text(values['lowest_price'][0]);
$(ul).append(li);
});
$('#result').html(html);
javascript jquery
I have a HTML template which looks like this:
<div id="amznplist">
<div id="amznpitem">
<span>
<img src="%%Produktbild%%" alt="%%Produkttitel%%"><div id="amzntitle">%%Produkttitel%%</div><div id="amzprice">%%Produktpreis%%</div>
</span>
</div>
</div>
What I try to archive is to load the entire html, copy the #amznpitem item, run through my response (data) and populate each li element with the data and add it to the end. What happens is that only one element is added to #amznplist. This is my current code.
var li = html.find('#amznpitem').clone();
html.find('#amznpitem').remove();
var ul = html.find('#amznplist');
$.each(data, function (index, values) {
li.find('#amzntitle').text(values['title'][0]);
li.find('a').attr('href', values['url'][0]);
li.find('img').attr('src', values['img'][0]);
li.find('img').attr('alt', values['title'][0]);
li.find('#amzprice').text(values['lowest_price'][0]);
$(ul).append(li);
});
$('#result').html(html);
javascript jquery
javascript jquery
asked Nov 21 '18 at 17:07


SvenSven
1
1
use classes instead of ids - ids are meant to be unique and as such jquery will only fetch the first element with that id it finds
– Pete
Nov 21 '18 at 17:17
add a comment |
use classes instead of ids - ids are meant to be unique and as such jquery will only fetch the first element with that id it finds
– Pete
Nov 21 '18 at 17:17
use classes instead of ids - ids are meant to be unique and as such jquery will only fetch the first element with that id it finds
– Pete
Nov 21 '18 at 17:17
use classes instead of ids - ids are meant to be unique and as such jquery will only fetch the first element with that id it finds
– Pete
Nov 21 '18 at 17:17
add a comment |
1 Answer
1
active
oldest
votes
You are not iterating over the response data in your loop, you are always accessing the first element - values...[0]
. Use the index that you have available in the each
function to pass the values from the current iteration
$.each(data, function (index, values) {
li.find('#amzntitle').text(values['title'][index]);
li.find('a').attr('href', values['url'][index]);
li.find('img').attr('src', values['img'][index]);
li.find('img').attr('alt', values['title'][index]);
li.find('#amzprice').text(values['lowest_price'][index]);
$(ul).append(li);
});
Thank you, but this is not the problem I try to solve. The [0] is important because the json is kinda weird at this point and I need to get the first child of values['title] and so on.
– Sven
Nov 22 '18 at 5:19
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417245%2fclone-element-and-append-to-end%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are not iterating over the response data in your loop, you are always accessing the first element - values...[0]
. Use the index that you have available in the each
function to pass the values from the current iteration
$.each(data, function (index, values) {
li.find('#amzntitle').text(values['title'][index]);
li.find('a').attr('href', values['url'][index]);
li.find('img').attr('src', values['img'][index]);
li.find('img').attr('alt', values['title'][index]);
li.find('#amzprice').text(values['lowest_price'][index]);
$(ul).append(li);
});
Thank you, but this is not the problem I try to solve. The [0] is important because the json is kinda weird at this point and I need to get the first child of values['title] and so on.
– Sven
Nov 22 '18 at 5:19
add a comment |
You are not iterating over the response data in your loop, you are always accessing the first element - values...[0]
. Use the index that you have available in the each
function to pass the values from the current iteration
$.each(data, function (index, values) {
li.find('#amzntitle').text(values['title'][index]);
li.find('a').attr('href', values['url'][index]);
li.find('img').attr('src', values['img'][index]);
li.find('img').attr('alt', values['title'][index]);
li.find('#amzprice').text(values['lowest_price'][index]);
$(ul).append(li);
});
Thank you, but this is not the problem I try to solve. The [0] is important because the json is kinda weird at this point and I need to get the first child of values['title] and so on.
– Sven
Nov 22 '18 at 5:19
add a comment |
You are not iterating over the response data in your loop, you are always accessing the first element - values...[0]
. Use the index that you have available in the each
function to pass the values from the current iteration
$.each(data, function (index, values) {
li.find('#amzntitle').text(values['title'][index]);
li.find('a').attr('href', values['url'][index]);
li.find('img').attr('src', values['img'][index]);
li.find('img').attr('alt', values['title'][index]);
li.find('#amzprice').text(values['lowest_price'][index]);
$(ul).append(li);
});
You are not iterating over the response data in your loop, you are always accessing the first element - values...[0]
. Use the index that you have available in the each
function to pass the values from the current iteration
$.each(data, function (index, values) {
li.find('#amzntitle').text(values['title'][index]);
li.find('a').attr('href', values['url'][index]);
li.find('img').attr('src', values['img'][index]);
li.find('img').attr('alt', values['title'][index]);
li.find('#amzprice').text(values['lowest_price'][index]);
$(ul).append(li);
});
answered Nov 21 '18 at 17:15
Velimir TchatchevskyVelimir Tchatchevsky
2,17411118
2,17411118
Thank you, but this is not the problem I try to solve. The [0] is important because the json is kinda weird at this point and I need to get the first child of values['title] and so on.
– Sven
Nov 22 '18 at 5:19
add a comment |
Thank you, but this is not the problem I try to solve. The [0] is important because the json is kinda weird at this point and I need to get the first child of values['title] and so on.
– Sven
Nov 22 '18 at 5:19
Thank you, but this is not the problem I try to solve. The [0] is important because the json is kinda weird at this point and I need to get the first child of values['title] and so on.
– Sven
Nov 22 '18 at 5:19
Thank you, but this is not the problem I try to solve. The [0] is important because the json is kinda weird at this point and I need to get the first child of values['title] and so on.
– Sven
Nov 22 '18 at 5:19
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417245%2fclone-element-and-append-to-end%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
use classes instead of ids - ids are meant to be unique and as such jquery will only fetch the first element with that id it finds
– Pete
Nov 21 '18 at 17:17