If you are dealing with tables or any other HTML element using jQuery you may want to implement a multilevel append function. And you want the low-level append to be dynamic data. Then how to add a loop inside jQuery’s append function?
Add loop inside jQuery append method.
Simply, You can’t add a loop inside append function! š
But there is a way to do it š First, declare a string variable and concatenate everything you want to be appended. then use that string as part of the appended content. Here is a simple example.
//#weekDays:we want to loop this array
var weekDays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
$.each(weekDays, function (index, row) {
var $dataToBeAppended += `<tr><td>${row}</td></tr>`;
});
$("#tbody").append($dataToBeAppended);
Ā
0 Comments