Chapter 3 DynaScript Predefined Objects


DOMNamedNodeMap methods

The DOMNamedNodeMap object has these methods:

getNamedItem method

Syntax

DOMNamedNodeMap.getNamedItem( name )

Description

Retrieves a DOMNode specified by name.

Return

A DOMNode object or null if the specified name did not identify any node in the map.

Example

This example creates a DOMNamedNodeMap named attlist from the attributes property of an element, and then writes out the value of the attribute with name attName.

attlist = elem.attributes
att = attlist.getNamedItem( attName );
document.writeln( att.value );

See also

"DOMNode object".

item method

Syntax

DOMNamedNodeMap.item( index )

Description

Returns the index item in the map. If index is greater than or equal to the number of nodes in the map, null is returned. Valid values for index are 0 through length - 1.

Return

A DOMNode object or null if the index is invalid.

Example

This script fragment loops over the items of a DOMNamedNodeMap object and assigns each node to a variable named thisNode.

for ( i = 0; i < nodemap.length; i++ ){
thisNode = nodemap.item( i );
}

removeNamedItem method

Syntax

DOMNamedNodeMap.removeNamedItem( name )

Description

Removes a DOMNode object specified by name.

Return

The DOMNode object that was removed or null if the specified name did not identify any node in the map.

Example

This example removes the attribute named attType from each Region element, and prints out messages for debugging purposes.

function RemoveNamedItem( domDoc, attType ){
elemlist = domDoc.getElementsByTagName( "Region" )
for( iElem=0; iElem < elemlist.length; iElem++ ) {
elem = elemlist.item( iElem );
attlist = elem.attributes
node = attlist.removeNamedItem( attType );
if( node == null ) {
document.writeln( "No attribute removed" );
} else {
document.writeln( "Removed attribute with value"
+ node.nodeValue );
}
}
document.writeln( domDoc.prettyPrint() );
}

See also

"DOMNode object"

setNamedItem method

Syntax

DOMNamedNodeMap.setNamedItem( node-name )

Description

Adds a node using its nodeName property. The node-name parameter is a DOMNode object to be stored in the named node map. The node will later be accessible using the value of the node's nodeName property passed to the getNamedItem method.

Return

If the new node replaces an existing node with the same name, the previously existing node is returned as a DOMNode object, otherwise null is returned.

Example

This example replaces an attribute node with one of the same name, but a changed value.

function SetNamedItem( domDoc, attType ){
elemlist = domDoc.getElementsByTagName( "Region" )
for( iElem=0; iElem < elemlist.length; iElem++ ) {
elem = elemlist.item( iElem );
attlist = elem.attributes
att = attlist.getNamedItem( attType );
att.value = "County";
oldNode = attlist.setNamedItem( att.name );
if( oldNode == null ) {
document.writeln( "New attribute added" );
} else {
document.writeln( "Replaced attribute" );
}
}
}

 


Copyright © 1999 Sybase, Inc. All rights reserved.