1 function insertAfter(newElement, targetElement) { 2 var parent = targetElement.parentNode; 3 if (parent.lastChild == targetElement) { 4 parent.appendChild(newElement); 5 } else { 6 parent.insertBefore(newElement, targetElement.nextSibling); 7 } 8 }