Nossos voluntários ainda não traduziram este artigo para o Português (do Brasil) . Junte-se a nós e ajude a fazer o trabalho!
The Element.getElementsByTagName()
method returns a live HTMLCollection
of elements with the given tag name. The subtree underneath the specified element is searched, excluding the element itself. The returned list is live, meaning that it updates itself with the DOM tree automatically. Consequently, there is no need to call several times Element.getElementsByTagName()
with the same element and arguments.
When called on an HTML element in an HTML document, getElementsByTagName
put its argument in lower case before proceeding. This is undesirable when trying to match camel-cased SVG elements in a sub-tree in an HTML document. Element.getElementsByTagNameNS()
works in that case.
Element.getElementsByTagName
is similar to Document.getElementsByTagName()
, except that its search is restricted to those elements which are descendants of the specified element.
Syntax
elements = element.getElementsByTagName(tagName)
elements
is a liveHTMLCollection
of found elements in the order they appear in the sub-tree. If no elements were found, theHTMLCollection
is empty.element
is the element from where the search should start. Note that only the descendants of this element are included in the search, but not the element itself.tagName
is the qualified name to look for. The special string"*"
represents all elements. For compatibility with XHTML, lower-case should be used.
Example
// check the alignment on a number of cells in a table. var table = document.getElementById("forecast-table"); var cells = table.getElementsByTagName("td"); for (var i = 0; i < cells.length; i++) { var status = cells[i].getAttribute("data-status"); if ( status == "open" ) { // grab the data } }
Specifications
Specification | Status | Comment |
---|---|---|
DOM The definition of 'Element.getElementsByTagName()' in that specification. |
Living Standard | Changed the return value from NodeList to HTMLCollection |
Document Object Model (DOM) Level 3 Core Specification The definition of 'Element.getElementsByTagName()' in that specification. |
Recommendation | No change from Document Object Model (DOM) Level 2 Core Specification |
Document Object Model (DOM) Level 2 Core Specification The definition of 'Element.getElementsByTagName()' in that specification. |
Recommendation | No change from Document Object Model (DOM) Level 1 Specification |
Document Object Model (DOM) Level 1 Specification The definition of 'Element.getElementsByTagName()' in that specification. |
Recommendation | Initial definition |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 1.0 [2] | (Yes) [1] | 5.5 | (Yes) [2] | (Yes) [2] |
getElementsByTagName("*") | 1.0 | (Yes) | 6.0 | (Yes) | (Yes) |
Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) [1] | (Yes) | (Yes) | (Yes) |
[1] Prior to Firefox 19, this method was returning a NodeList
; it was then changed to reflect the spec change.
[2] Initially, this method was returning a NodeList
; it was then changed to reflect the spec change.