How to retrieve an element from another DOM element

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    How to retrieve an element from another DOM element

    Is there a method like document.getEle mentByID() which will retrieve an element from another DOM element?

    Rephrasing: Say I retrieve a Div element using the getElementID method, is there a method to retrieve an element from within that element?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Yes, getElementsByTa gName() and getElementsByNa me(). These will return an array, so you just need to index them, e.g.
    Code:
    elem.getElementsByTagName("a")[0]
    returns the first link element within elem.

    Edit: also see Navigating nodes
    Last edited by acoder; Mar 4 '09, 05:58 PM. Reason: Fixed correction of typo

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Thanks acoder

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        It's actually getElementsByNa me(). Having said that though, it wouldn't work here because it's a method of document only unlike getElementsByTa gName(). You can also use the childNodes property. With some newer browsers, you can also use getElementsByCl assName().

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          You can use the $find method if developing with the .NET Ajax Framework.

          Comment

          Working...