knot22 knot22 - 4 months ago 24
jQuery Question

translation of vanilla JavaScript to jQuery

Below is a JS function:

I'd like to update this so it uses jQuery but I am having difficulty figuring out how to translate it.

function pathContents(fileList) {
var list = document.createElement('ul');
for (file in fileList) {
var item = document.createElement('li');
item.appendChild(document.createTextNode(fileList[file]));
list.appendChild(item);
}
return list;
}

Answer

The jQuery equivalent "all-the-way" would be:

function pathContents(fileList) {
    var $list = $('<ul/>');
    $.each(fileList, function () {
        $('<li/>').text(this).appendTo($list);
    });
    return $list;
}

But the pure JavaScript way really is not that bad. For one, you cannot beat the performance of it.