Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery - Fastest possible way of appending list items to list

I have some jquery that works fine, but I'd like to highly optimize it. Basically I'm doing standard appending list items to unordered lists. Can anyone recommend the fastest way to optimise the following code e.g. createDocumentFragment ?

for (key in data) {
     li = $('<li><span class="item">' + data[key]["Name"] + '</span><img src=' +   options.deleteIcon + ' alt="remove" class="delete"/></li>');
     $('.item', li).data('ID', data[key]["Id"]);
     $(list).append(li);
}
like image 432
Click Ahead Avatar asked Nov 25 '10 01:11

Click Ahead


1 Answers

var sb = new Array();

for (key in data) {
   sb.push('<li><span class="item" id="', data[key]['Id'], '">', data[key]["Name"], '</span><img src=', options.deleteIcon, ' alt="remove" class="delete"/></li>')}

$(list).append(sb.join(""));
like image 185
tim Avatar answered Sep 23 '22 22:09

tim