Element.getElementsByTagName() (Method)

Obtain a collection of elements that have the same tag name.


DOM level - 1
JavaScript - 1.5
JScript - 5.0
Internet Explorer - 5.0
Netscape - 6.0
Opera - 5.0
Property/method value type:NodeList object
JavaScript syntax:-myElement.getElementsByTagName(aTagName)
Argument list:aTagNameThe name of a tag

Modern browsers are implementing a DOM standard architecture that represents a document in the same way regardless of the browser you are running. This means that the focus of script development will move away from a browser-centric view to a standards-centric approach. Using DOM will lead to you developing a much more portable script product.

One of the important "gateway" methods to this DOM model is getElementsByTagName(). This can be applied to a document or to an individual HTML element within it.

The getElementsByTagName() method returns a collection of elements that are children of the receiving element so long as they were instantiated by the specified tag name.

Many HTML tags create a container within which other tags are placed. <HTML> contains <HEAD> and <BODY> as its immediate children. A <BODY> tag might contain a <TABLE> which contains <TR> row elements, each of which has its own collection of <TD> elements. Within those table cells, further parent- child relationships are constructed.

The getElementsByTagName() method will walk down that parent-child tree structure and collect a reference to each Element object that was instantiated by the HTML tag name you specify. Then on return, it passes back a Collection object with them organized for easy access with the item() method or by array index.

Since you can apply this search to an individual HTML element, the traversal of the document tree can be very efficient because you only need to walk the portion local to the objects you want to find.


See also:Document.getElementsByTagName(), NodeList object