Name

XML.insertBefore( ) Method — give a node a new previous sibling

Availability

Flash 5

Synopsis

theNode.insertBefore(newChild, beforeChild)

Arguments

newChild

An existing XML node object.

beforeChild

The child of theNode before which newChild should be inserted.

Description

The insertBefore( ) method adds newChild to theNode’s child list, before beforeChild. The insertBefore( ) method is similar to appendChild( ) but lets us precisely position a new node in an existing XML object hierarchy.

Example

// Create a document
myDoc = new XML('<P>paragraph 2</P>');

// Create a P node and a text node
newP = myDoc.createElement("P");
newText = myDoc.createTextNode("paragraph 1");

// Append the text node to the P node
newP.appendChild(newText);

// Insert the new P node (including its text child) before the existing P
myDoc.insertBefore(newP, myDoc.firstChild);

trace(myDoc);  // Displays: "<P>paragraph 1</P><P>paragraph 2</P>"

See Also

XML.appendChild( )

Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.