Export (0) Print
Expand All

getElementsByTagName method

Retrieves a collection of objects based on the specified element name.

Document Object Model (DOM) Level 2 HTML Specification, Section 1.6.5

 

Syntax

object.getElementsByTagName(v)

Parameters

v [in]

Type: String

String that specifies the name of an element.

Return value

Type: Element

Returns a collection of objects with the specified element name.

Standards information

Remarks

The getElementsByTagName method is equivalent to using the tags method on the all collection. For example, the following code shows how to retrieve a collection of div elements from the body element, first using the Dynamic HTML (DHTML) Object Model and then the Document Object Model (DOM).

  • Using the DHTML Object Model:
    
    var aDivs = document.body.all.tags("div");
    
    
  • Using the DOM:
    
    var aDivs = document.body.getElementsByTagName("div");
    
    

When you use the getElementsByTagName method, all child and nested child elements with the specified tag name are returned. For example, all of the span elements in the following example would be returned by the getElementsByTagName method.



<div id="oDiv">
    <span>Immediate Child
        <div>
            <span>Child of Child div</span>
        </div>
    </span>
</div>
<script type="text/javascript">
    var aSpans = oDiv.getElementsByTagName("span");
</script>


Examples

The following example uses the getElementsByTagName method to return the children of a ul element based on the selected li element.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/getElementsByTagName.htm


<!DOCTYPE html>
<html>

<body>
  <p><strong>Click a list element:</strong></p>
  <ul id="outerUL">
    <li>Item 1
      <ul>
        <li>Sub Item 1.1
          <ol>
            <li>Super Sub Item 1.1</li>
            <li>Super Sub Item 1.2</li>
          </ol>
        </li>
        <li>Sub Item 1.2</li>
        <li>Sub Item 1.3</li>
      </ul>
    </li>
    <li>Item 2
      <ul>
        <li>Sub Item 2.1</li>
        <li>Sub Item 2.3</li>
      </ul>
    </li>
    <li>Item 3</li>
  </ul>
  <script>
    document.getElementById('outerUL').addEventListener('click', fnGetTags, false);

    function fnGetTags(evt) {
      var oWorkItem = evt.target;
      var aReturn = oWorkItem.parentElement.getElementsByTagName("li");

      alert("Length: "
         + aReturn.length
         + "\nFirst Item: "
         + aReturn[0].childNodes[0].nodeValue);
    }
  </script>
</body>

</html>

See also

a
abbr
acronym
address
applet
area
b
base
baseFont
bdo
bgSound
big
blockQuote
body
br
button
caption
center
cite
code
col
colGroup
custom
dd
del
dfn
dir
div
dl
document
dt
em
embed
fieldSet
font
form
frame
frameSet
head
hn
hr
html
i
iframe
img
ins
kbd
label
legend
li
link
listing
map
marquee
menu
ol
p
plainText
pre
q
s
samp
script
select
small
span
strike
strong
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
title
tr
tt
u
ul
var
xmp
About the W3C Document Object Model

 

 

Show:
© 2014 Microsoft