Q:

Insert a  inside an empty  or to the bottom if  exists with jQuery

I had a look around and (strangely) couldn't find this being asked before, so apologies if this is a duplicate.
I have an unordered list with the class .attachments, if it has no list items inside of it I want the code to insert the <li> inside of the <ul>, if any <li> exists (regardless of how many) I wish for it to be inserted at the bottom before the </ul>. How would I go about doing this?
I have the following code, however this would apply it after the </ul> instead of inside of it.
$("ul.attachments").after('<li></li>');

A:

Don't use .after(), use .append():
$("ul.attachments").append($("<li>"));

There's no special case when there are no items inside the <ul>, you just always insert it at the bottom. That's how the DOM tree works.

