knot22 knot22 - 4 months ago 16
Javascript Question

translation of JavaScript to jQuery

Below is a JS function:

I'd like to update this so it uses jQuery but 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(fileList[file]).appendTo($list);
    });
    return $list;
}

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

Comments