nionta's avatar

loop inside an append()

I want something like this:(though my code is not appropriate)

 branchObj.forEach(function (element) {
                                choice_per_question = element.choice_per_question;
                                total_question = element.total_question;
                                question_per_block = element.question_per_block;
                                no_of_td = total_question/question_per_block;
                                $('#ans_set_table').append(
                                    for(var i = 1;i<=question_per_block ;i++){
                                        '<tr>'+
                                       for(var j = 1;j<=no_of_td ;j++){
                                            '<td>'+
                                           for(var k = 1;k<=choice_per_question ;k++){
                                                '<input type="checkbox">'+
                                            };
                                            '</td>'+
                                        };
                                        '</tr>'
                                    };
                               );
                            });

how can I use a loop inside an append()??? can anyone help me?

0 likes
1 reply
Cronix's avatar
Cronix
Best Answer
Level 67

create the html in the loop, assigning it to a variable. Then at the end of the loop, append it.

for (var i = 1; i<=question_per_block; i++){
    var html = '<tr>';

    for (var j = 1; j<=no_of_td; j++){
        html += '<td>';

        for (var k = 1; k<=choice_per_question; k++){
            html += '<input type="checkbox">';
        };

        html += '</td>';
    };

    html += '</tr>';

    // now that the html table row is built, append it
    $('#ans_set_table').append(html);
};
1 like

Please or to participate in this conversation.