asp.net.ph

Skip Navigation Links

getElementsByTagName Method

DHTML Methods


DOM Level 1 Specification.

Returns a list of all descendant elements that match the supplied name.

Syntax

objNodeList = object.getElementsByTagName ( tagName ) 
Parameters

tagName String specifying the name of the element to find. The string "*" matches all descendant elements of this element.

Return Value

Returns a collection ( NodeList object ) containing all elements that match the supplied name.

Remarks

Elements appear in the order encountered in a preorder traversal of this element’s tree.

Note that a NodeList object is returned even if there are no matches. In such a case, the length of the list will be set to zero.

The DOM NodeList object is live and immediately reflects changes to the nodes that appear in the list.

Example

The following example shows use of the getElementsByTagName method to create a NodeList of all level 5 headings ( H5 elements ) in this document, and displays each object’s nodeTtype and nodeValue in sequence:

function getHeaders ( ) {
var childs, tags = document.getElementsByTagName ( 'h5' );
if ( tags.length != 0 )
  for ( t = 0; t < tags.length; t++ ) {
    childs = tags [ t ] .childNodes;
    for ( c = 0; c < childs.length; c++ )
      alert ( tags [ t ] .tagName + ' ' + t + ' Child ' + c + 
        '\nnodeType " + childs [ c ] .nodeType + 
        '\nnodeValue " + childs [ c ] .nodeValue )
  }
}

 Show me 

See Also

getElementById



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note