Chapter 3 DynaScript Predefined Objects


DOMNode properties

The DOMNode object has these properties:

attributes property

Syntax

DOMNode.attributes

Description

A DOMNamedNodeMap containing the attributes of this node. This property is applicable only to nodes that are elements.

Example

This fragment tests to see if a node is an element, and assigns the attributes of the element to a DOMNamedNodeMap named attlist.

if( node.nodeType == 1 ){ // Element
attlist = node.attributes
} else {
document.writeln( "Not an element" );
}

See also

"DOMNamedNodeMap object".

childNodes property

Syntax

DOMNode.childNodes

Description

A DOMNodeList object that contains all the children of this node. If there are no children, then this is a node list containing zero nodes.

Note  

Performance tip
To loop over a childNodes list, it is more efficient to use the nextSibling property than to explicitly use the childNodes list of the parent object.

Not all nodes can have children. This table lists the types of children that are valid for each type of node.

Node Type

Node Types Valid for Children

document

element (maximum of one), processing instruction, comment, document type

document fragment

element, processing instruction, comment, text, CDATA section, entity reference

document type

no children

entity reference

element, processing instruction, comment, text, CDATA section, entity reference

element

element, text, comment, processing instruction, CDATA section, entity reference

attribute

text, entity reference

processing instruction

no children

comment

no children

text

no children

CDATA section

no children

entity

element, processing instruction, comment, text, CDATA section, entity reference

notation

no children

Example

This example loops through the childNodes of a node.

function ChildNodes( domDoc ){
docElem = domDoc.documentElement;
var node = docElem.firstChild;
do {
document.writeln( node.nodeName );
node = node.nextSibling;
} while ( node != null )
}

firstChild property

Syntax

DOMNode.firstChild

Description

The DOMNode object representing the first child of this node, or null if there are no children.

Example

This fragment sets the variable node to the first child of the document element.

var docElem = domDoc.documentElement;
var node = docElem.firstChild;

lastChild property

Syntax

DOMNode.lastChild

Description

The DOMNode object representing the last child of this node, or null if there are no children.

Example

This fragment sets the variable node to the last child of the document element.

var docElem = domDoc.documentElement;
var node = docElem.lastChild;

nextSibling property

Syntax

DOMNode.nextSibling

Description

The DOMNode object representing the node immediately following this node, or null if this is the last node.

Example

This fragment loops over the siblings of an element named node.

do {
document.writeln( node.nodeName );
node = node.nextSibling;
} while ( node != null )

nodeName property

Syntax

DOMNode.nodeName

Description

The name of this node. The name of the node depends on the node type. This table lists the contents of the nodeName property for each node type.

Node Type

Contents of nodeName property

document

"document"

document fragment

"document-fragment"

document type

document type name

entity reference

name of entity referenced

element

tag name

attribute

name of attribute

processing instruction

target

comment

"comment"

text

"text"

CDATA section

"cdata-section"

entity

entity name

notation

notation name

XML is case sensitive. If you check the name of a node in a comparison, you must ensure the check is case sensitive.

Example

This fragment writes out the name of each of a set of nodes, held in a variable named node.

do {
document.writeln( node.nodeName );
node = node.nextSibling;
} while ( node != null )

See also

"getElementsByTagName method".

nodeType property

Syntax

DOMNode.nodeType

Description

An integer representing the type of node object. This table lists the nodeType number for each type of node.

Node Type

Contents of nodeType property

element

1

attribute

2

text

3

CDATA section

4

entity reference

5

entity

6

processing instruction

7

comment

8

document

9

document type

10

document fragment

11

notation

12

Example

This fragment performs different operations based on the type of node. The node is held in a variable named domNode.

switch domNode.nodeType {
case 1:
//element operations
break;
case 2:
//attribute operations
break;
case 3:
//text operations
break;
case 4:
//CDATA section operations
break;
case 5:
//entity reference operations
break;
case 6:
//entity operations
break;
case 7:
// processing instruction operations
break;
case 8:
//comment operations
break;
case 9:
//document operations
break;
case 10:
// document type operations
break;
case 11:
// document fragment operations
break;
case 12:
//notation operations
break;
default:
document.writeln( "Invalid node type: " +
domChild.nodeType );
}

nodeValue property

Syntax

DOMNode.nodeValue

Description

The value of this node. The value depends on the type of node. This table lists the contents of the nodeValue property for each node type.

Node Type

Contents of nodeValue property

document

null

document fragment

null

document type

null

entity reference

null

element

null

attribute

value of attribute

processing instruction

entire content excluding the target

comment

content of the comment

text

content of the text node

CDATA section

content of the CDATA section

entity

null

notation

null

Example

This fragment writes out the nodeValue for an attribute named Type:

if( domAtt.nodeName == "Type" ){
document.writeln( domAtt.nodeValue );
}

ownerDocument property

Syntax

DOMNode.ownerDocument

Description

The DOMDocument object associated with this node. For DOMDocument objects, this property is null.

Example

This fragment writes out the name of the document element for the DOMDocument object that owns domNode.

var domDoc = domNode.ownerDocument;
document.writeln( domDoc.documentElement.nodeName );

See also

"DOMDocument object".

parentNode property

Syntax

DOMNode.parentNode

Description

The DOMNode object representing the parent of this node, or null if there is no parent. This node objects do not have a parent: DOMDocument , DOMDocumentFragment , DOMAttribute, DOMEntity, DOMDocumentType, and DOMNotation .

Example

This example writes out the parent of the node held in the variable domNode, if it exists.

if( domNode.parentNode != null ){
document.writeln( domNode.parentNode.nodeName;
}

previousSibling property

Syntax

DOMNode.previousSibling

Description

The DOMNode object representing the node immediately preceding this node, or null if no nodes precede this node.

Example

This function loops backwards through the children of a node.

function BackwardsChildNodes( domNode ){
var domChild = domNode.lastChild;
do {
document.writeln( domChild.nodeName );
domChild = domChild.previousSibling;
} while ( domChild != null )
}

 


Copyright © 1999 Sybase, Inc. All rights reserved.