Javascript Append Child AFTER Element

You can use: if (parentGuest.nextSibling) { parentGuest.parentNode.insertBefore(childGuest, parentGuest.nextSibling); } else { parentGuest.parentNode.appendChild(childGuest); } But as Pavel pointed out, the referenceElement can be null/undefined, and if so, insertBefore behaves just like appendChild. So the following is equivalent to the above: parentGuest.parentNode.insertBefore(childGuest, parentGuest.nextSibling);

javascript to create a button with onclick

function createButton(context, func) { var button = document.createElement(“input”); button.type = “button”; button.value = “im a button”; button.onclick = func; context.appendChild(button); } window.onload = function() { createButton(document.body, function() { highlight(this.parentNode.childNodes[1]); // Example of different context, copied function etc // createButton(this.parentNode, this.onclick); }); }; Is that what you want?

Advantages of createElement over innerHTML?

There are several advantages to using createElement instead of modifying innerHTML (as opposed to just throwing away what’s already there and replacing it) besides safety, like Pekka already mentioned: Preserves existing references to DOM elements when appending elements When you append to (or otherwise modify) innerHTML, all the DOM nodes inside that element have to … Read more