Availability
DOM Level 1 Core
Synopsis
Node[] getElementsByTagName(String tagname);
Arguments
tagname
The tag name of the Element nodes to be returned, or the wildcard
string "*" to return all Element nodes in the document
regardless of tag name. For HTML documents, tag names are compared in
a case-insensitive fashion.
Returns
A read-only array (technically, a NodeList) of all Element nodes in
the document tree with the specified tag name. The returned Element
nodes are in the same order in which they appear in the document
source.
Description
This method returns a NodeList (which you can treat as a read-only
array) that contains all Element nodes from the document that have
the specified tag name, in the order in which they appear in the
document source. The NodeList is "live"; i.e., its
contents are automatically updated as necessary if elements with the
specified tag name are added to or removed from the document.
HTML documents are case-insensitive, and you can specify
tagname using any capitalization; it
matches all tags with the same name in the document, regardless of
how those tags are capitalized in the document source. XML documents,
on the other hand, are case-sensitive, and
tagname matches only tags with the same
name and exactly the same capitalization in the document source.
Note that the Element interface defines a method by the same name
that searches only a subtree of the document. Also, the HTMLDocument
interface defines getElementsByName( ), which
searches for elements based on the value of their
name attributes rather than their tag names.
Example
You can find and iterate through all <h1>
tags in a document with code like the following:
var headings = document.getElementsByTagName("h1");
for(var i = 0; i < headings.length; i++) { // Loop through the returned tags
var h = headings[i];
// Now do something with the <h1> element in the h variable
}
See Also
Document.getElementById( ), Element.getElementsByTagName( ),
HTMLDocument.getElementsByName( )