javascript - Find element then append in loop - Stack Overflow

I want to dynamically create html element, but when trying to append some element inside the container

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 the html variable, but you never append that to the DOM, hence nothing actually appears to happen – Rory McCrossan Commented Jun 2, 2016 at 15:48
Add a ment  | 

3 Answers 3

Reset to default 4

This 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);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745196448a4616112.html

相关推荐

  • javascript - Find element then append in loop - Stack Overflow

    I want to dynamically create html element, but when trying to append some element inside the container

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信