admin管理员组文章数量:1415664
I want to dynamically create html element, but when trying to append some element inside the container in loop it is not working. Why?
var html = '<div id="' + question.id + '"> \
<div class="quizlib-question"> \
<div class="well well-sm quizlib-question-title"><strong>' + question.title + '</strong></div> \
<div class="quizlib-question-answers"></div> \
</div> \
</div>';
for (var i=0; i < question.choices.length; i++) {
var answerHTML = '<div class="radio quizlib-question-answer"><label><input type="radio" name="' + question.name + '" value="' + i + '">' + question.choices[i] + '</label></div>';
//this part is nor working
$(html).find(".quizlib-question-answers").append(answerHTML);
}
$('body').append(html);
I want to dynamically create html element, but when trying to append some element inside the container in loop it is not working. Why?
var html = '<div id="' + question.id + '"> \
<div class="quizlib-question"> \
<div class="well well-sm quizlib-question-title"><strong>' + question.title + '</strong></div> \
<div class="quizlib-question-answers"></div> \
</div> \
</div>';
for (var i=0; i < question.choices.length; i++) {
var answerHTML = '<div class="radio quizlib-question-answer"><label><input type="radio" name="' + question.name + '" value="' + i + '">' + question.choices[i] + '</label></div>';
//this part is nor working
$(html).find(".quizlib-question-answers").append(answerHTML);
}
$('body').append(html);
Share
Improve this question
edited Jun 2, 2016 at 15:50
j08691
208k32 gold badges269 silver badges280 bronze badges
asked Jun 2, 2016 at 15:46
Below the RadarBelow the Radar
7,63511 gold badges69 silver badges145 bronze badges
1
-
2
You are appending the
answerHtml
to a jQuery object which contains thehtml
variable, but you never append that to the DOM, hence nothing actually appears to happen – Rory McCrossan Commented Jun 2, 2016 at 15:48
3 Answers
Reset to default 4This line:
$(html).find(".quizlib-question-answers").append(answerHTML);
does not update the string variable html
, it appends to a jQuery object which has been created initially with the contents of html
.
To fix this, append the jQuery object to the dom, not html
. Start by creating the jQuery object initially
var $html = $('<div id="' + question.id + '"> \
<div class="quizlib-question"> \
<div class="well well-sm quizlib-question-title"><strong>' + question.title + '</strong></div> \
<div class="quizlib-question-answers"></div> \
</div> \
</div>');
(Note, I prefix jQuery objects with $
by convention)
Then append as normal:
for (var i=0; i < question.choices.length; i++) {
var answerHTML = '<div class="radio quizlib-question-answer"><label><input type="radio" name="' + question.name + '" value="' + i + '">' + question.choices[i] + '</label></div>';
//this part is nor working
$html.find(".quizlib-question-answers").append(answerHTML);
}
Finally add that to the DOM
$('body').append($html);
The problem is because you are appending html
to the DOM, yet you never update the html
variable. Instead you're creating a jQuery object from that variable, but you never do anything with it.
To fix this, try creating a jQuery object from html
immediately and then append that to the DOM before doing your loop, like this:
var $html = $('<div id="' + question.id + '"> \
<div class="quizlib-question"> \
<div class="well well-sm quizlib-question-title"><strong>' + question.title + '</strong></div> \
<div class="quizlib-question-answers"></div> \
</div> \
</div>').appendTo('body');
for (var i = 0; i < question.choices.length; i++) {
var answerHTML = '<div class="radio quizlib-question-answer"><label><input type="radio" name="' + question.name + '" value="' + i + '">' + question.choices[i] + '</label></div>';
$html.find(".quizlib-question-answers").append(answerHTML);
}
Working example
Like the menter mentioned, you have to append to the body before you start searching for those DOM elements you created. For example:
var html = '<div id="' + question.id + '"> \
<div class="quizlib-question"> \
<div class="well well-sm quizlib-question-title"><strong>' + question.title + '</strong></div> \
<div class="quizlib-question-answers"></div> \
</div> \
</div>';
$('body').append(html);
for (var i=0; i < question.choices.length; i++) {
var answerHTML = '<div class="radio quizlib-question-answer"><label><input type="radio" name="' + question.name + '" value="' + i + '">' + question.choices[i] + '</label></div>';
//this part is nor working
$(".quizlib-question-answers").append(answerHTML);
}
本文标签: javascriptFind element then append in loopStack Overflow
版权声明:本文标题:javascript - Find element then append in loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745196448a2647159.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论