Return to Snippet

Revision: 2350
at February 3, 2007 15:03 by 1man


Initial Code
//create function, it expects 2 values.
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);
		}
}

Initial URL

                                

Initial Description
Vert useful function since there isn't an insertAfter function in the DOM. Call it inside scripts, it expects
insertAfter(*the new element to be inserted*, *the element you want it to be inserted after*);

Initial Title
insertAfter function for the DOM

Initial Tags
DOM, function

Initial Language
JavaScript