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( )
•
Table of Contents
•
Index
•
Reviews
•
Examples
•
Reader Reviews
•
Errata
JavaScript: The Definitive Guide, 4th Edition
By
David Flanagan
Publisher
: O'Reilly
Pub Date
: November 2001
ISBN
: 0-596-00048-0
Pages
: 936
Slots
: 1
This fourth edition of the definitive reference to
JavaScript, a scripting language that can be embedded
directly in web pages, covers the latest version of the
language, JavaScript 1.5, as supported by Netscape 6 and
Internet Explorer 6. The book also provides complete
coverage of the W3C DOM standard (Level 1 and Level 2),
while retaining material on the legacy Level 0 DOM for
backward compatibility.