Seven years, but I tought I could help anyone else that comes across a similar problem. For example, if you want to use JQuery's $.each() with .append() to make an list of users, such as:
<ul>
<li>User 1</li>
<li>User 2</li>
</ul>
Here's what you DON'T want to do:
element.append(`<ul>`);
$.each(users, function(key, value) {
element.append(`<li>${value.name}</li>`);
});
element.append(`</ul>`);
The output will be something like:
<ul></ul>
<li>User 1</li>
<li>User 2</li>
Instead, here's what you WANT to do:
element.append(`<ul id="ul-users"></ul>`);
$.each(users, function(key, value) {
$("#ul-users").append(`<li>${value.name}</li>`);
});
So the output will be as follows:
<ul id="ul-users">
<li>User 1</li>
<li>User 2</li>
</ul>