If you are looking for a plain JS solution, then you just use insertBefore()
against nextSibling
.
Something like:
parentGuest.parentNode.insertBefore(childGuest, parentGuest.nextSibling);
Note that default value of nextSibling
is null
, so, you don't need to do anything special for that.
Update: You don't even need the if
checking presence of parentGuest.nextSibling
like the currently accepted answer does, because if there's no next sibling, it will return null
, and passing null
to the 2nd argument of insertBefore()
means: append at the end.
Reference:
.
IF you are using jQuery (ignore otherwise, I have stated plain JS answer above), you can leverage the convenient after()
method:
$("#one").after("<li id='two'>");
Reference: