getElementsByTagName Method

MS Office DHTML, HTML & CSS

getElementsByTagName Method

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

Syntax

collObjects = document.getElementsByTagName(sTagName)

Parameters

sTagNameRequired. String that specifies the name of an element.

Return Value

Returns a collection of objects with the specified element name.

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 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 elements with the specified tag name are returned.

Example

This example uses the getElementsByTagName method to return the children of a UL element based on the selected LI element.

Sample Code

<SCRIPT>
function fnGetTags(){
   var oWorkItem=event.srcElement;
   var aReturn=oWorkItem.parentElement.getElementsByTagName("LI");
   alert("Length: "
      + aReturn.length
      + "\nFirst Item: "
      + aReturn[0].childNodes[0].nodeValue);
}
</SCRIPT>
<UL onclick="fnGetTags()">
<LI>Item 1
   <UL>
      <LI>Sub Item 1.1
      <OL>
         <LI>Super Sub Item 1.1
         <LI>Super Sub Item 1.2
      </OL>
      <LI>Sub Item 1.2
      <LI>Sub Item 1.3
   </UL>		
<LI>Item 2
   <UL>
      <LI>Sub Item 2.1
      <LI>Sub Item 2.3
   </UL>
<LI>Item 3
</UL>
This feature requires Internet Explorer 5 or later. Click the icon below to install the latest version. Then reload this page to view the sample.
Microsoft Internet Explorer

Applies To

[ Object Name ]
PlatformVersion
Win16:
Win32:
Mac:
Unix:
WinCE:
Version data is listed when the mouse hovers over a link, or the link has focus.
A, 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

See Also

document object model overviewInternet Link


Back to topBack to top

Did you find this topic useful? Suggestions for other topics? write us!Internet Link

© 1999 microsoft corporation. all rights reserved. terms of useInternet Link.