Chapter 3 DynaScript Predefined Objects


DOMNode methods

The DOMNode object has these methods:

appendChild method

Syntax

DOMNode.appendChild( newChild )

Description

Adds the DOMNode newChild to the end of the list of children of this node. If newChild already exists in the tree, it is first removed.

Return

The DOMNode which was added or null if the node could not be appended (for example, if newChild is not a valid type of child for this node).

Example

This fragment adds a child element to a node held in the customer variable.

new_elem = domDoc.createElement( "Name" );
new_text = domDoc.createTextNode("Ann T. Dote" );
el = customer.appendChild( new_elem );
el.appendChild( new_text );

cloneNode method

Syntax

DOMNode.cloneNode( deep )

Description

Returns a duplicate of this DOMNode . The duplicate node has no parent. deep is a Boolean indicating whether to clone the subtree under this node.

For DOMElement objects, all attributes and their values are copied.

Return

The duplicate DOMNode .

Example

This fragment makes a duplicate of the first child of a document element (and its subtree) and adds it to the end of the list of its children. The call to prettyPrint is for debugging purposes.

var docElem = domDoc.documentElement;
var thisNode = docElem.firstChild;
var newNode = thisNode.cloneNode( true );
docElem.appendChild( newNode );
document.writeln( domDoc.prettyPrint() );

hasChildNodes method

Syntax

DOMNode.hasChildNodes( )

Description

Indicates whether a node has any children

Return

Boolean.

Example

This statement tests to see whether thisNode has any children.

if( thisNode.hasChildNodes() ){
// operations here
}

insertBefore method

Syntax

DOMNode.insertBefore( newChild, refChild )

Description

Inserts the DOMNode newChild before the existing child DOMNode refChild. If refChild is null, newChild is inserted at the end of the list of children.

If newChild is a DOMDocumentFragment , all of its children are inserted, in order, before refChild. If newChild already exists in the tree, it is first removed.

Return

The inserted DOMNode , or null if the node could not be inserted (for example, if refChild is not a child of this node).

Example

This fragment makes a duplicate of the first child of a document element (and its subtree) and adds it as the penultimate child. The call to prettyPrint is for debugging purposes.

var docElem = domDoc.documentElement;
var thisNode = docElem.firstChild;
var newNode = thisNode.cloneNode( true );
docElem.insertBefore( newNode, docElem.lastChild );
document.writeln( domDoc.prettyPrint() );

removeChild method

Syntax

DOMNode.removeChild( oldChild )

Description

Removes from the list of children, the child node indicated by oldChild.

Return

The removed DOMNode or null if the child could not be removed (for example, if oldChild is not a child of this node.

Example

This fragment removes the last child of the document element from a document.

var docElem = domDoc.documentElement;
var thisNode = docElem.lastChild;
var oldNode = docElem.removeChild( thisNode );

replaceChild method

Syntax

DOMNode.replaceChild( newChild, oldChild )

Description

Replaces the child DOMNode oldChild with the DOMNode newChild in the list of children. If newChild already exists in the tree, it is first removed.

Return

The replaced DOMNode (oldChild) or null if the node could not be replaced (for example, if oldChild is not a child of this node).

Example

This fragment replaces the last child of the document element with the first child. The first child is removed from its place in the beginning of the list.

var thisNode = docElem.firstChild;
var oldNode = docElem.replaceChild( thisNode,
docElem.lastChild );

 


Copyright © 1999 Sybase, Inc. All rights reserved.