www.fgks.org   »   [go: up one dir, main page]

Click to Rate and Give Feedback
Methods
 getElementsByTagName Method
getElementsByTagName Method

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

Syntax

collObjects = object.getElementsByTagName(sTagName)

Parameters

sTagName Required. 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 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.


<SCRIPT>
var aSpans = oDiv.getElementsByTagName("SPAN");
</SCRIPT>
<DIV id="oDiv">
    <SPAN>Immediate Child
        <DIV>
            <SPAN>Child of Child DIV</SPAN>
        </DIV>
    </SPAN>
</DIV>

Example

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

<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 >
<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 Microsoft® Internet Explorer 5 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

Standards Information

This method is defined in World Wide Web Consortium (W3C) Document Object Model (DOM) Level 1 World Wide Web link.

Applies To

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

See Also

About the W3C Document Object Model
Tags What's this?: Add a tag
Community Content
 
Add Community Content
missing element in getElementsByName bug      Sasha Firsov ... John Sudds - MSFT   |   Edit   |  

The IE7 bug does not allow to safely use this method: in returned result, one element is missing.

Test case: dynamically create 1000 same custom tags

<customtag><A name="same4all">aa</a></customtag>

the getElementsByName gave 1000 as expected, document.getElementsByTagName and document.all.tags returns 999 elements.

testcase:

http://simulationlabs.com/test/NavigationTest.html

----

[jsudds.msft] Thanks for the report. I'll make sure the product team knows about it. Fortunately, this behavior is limited to custom tags only; getElementsByTagName works as expected for built-in elements such as the A tag.

Tags What's this?: html (x) Add a tag
Wildcard '*' is allowed      John Sudds - MSFT   |   Edit   |  
To return all descendant elements, specify an asterisk (*) as sTagName. The resulting array will contain only elements and no text nodes.
Tags What's this?: dhtml (x) Add a tag
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker
DCSIMG