The example I see posted all of the time seems like it's suboptimal, because it involves concatenating strings, which seems so not jQuery. It usually looks like this:
$.getJSON("/Admin/GetFolderList/", function(result) {
for (var i = 0; i < result.length; i++) {
options += '<option value="' + result[i].ImageFolderID + '">' + result[i].Name + '</option>';
}
});
Is there a better way?
I found this to be working from jquery site
I've read that using document fragments is performant because it avoids page reflow upon each insertion of DOM element, it's also well supported by all browsers (even IE 6).
I first read about this in CodeSchool's JavaScript Best Practices course.
Here's a comparison of different approaches, thanks go to the author.
Or maybe:
I use the selectboxes jquery plugin. It turns your example into:
Andreas Grech was pretty close... it's actually
this
(note the reference tothis
instead of the item in the loop):The fastest way is this:
According to this link is the fastest way because you wrap everything in a single element when doing any kind of DOM insertion.