Handy function absent from javascript’s built in DOM functions. It allows you to insert a new node after another. Originally posted by 1man on snipplr.com.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function insertAfter(newElement, targetElement) { // target is what you want it to go after. Look for this elements parent. var parent = targetElement.parentNode; // if the parents lastchild is the targetElement... if (parent.lastchild == targetElement) { // add the newElement after the target element. parent.appendChild(newElement); } else { // else the target has siblings, insert the new element between the target and it's next sibling. parent.insertBefore(newElement, targetElement.nextSibling); } } |
Recent Comments