Check if element is child of another element

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

    Check if element is child of another element

    I have a quick question...

    Before I start looping through all of the child elements of a <div> I wanted to ask if there is a better way to check if an element is a child of another element?

    I thought I found a method (the contains(pchild ) method) but didn't find any documentation for this method and apparently it doesn't work the way I thought it did.

    Thanks for your time

    -Frinny
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    just check out the nodeType property (it’s 3 for elements)

    if you want to work with NodeLists, you can also use element.getElem entsByTagName(" *");

    Originally posted by Frinny
    if an element is a child of another element
    an element is always a child of another element (except the <html> element)

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      I'll check out the nodeType property....did n't see it listed as a property of the <div> element though.

      I just wish there was something like the document.getEle mentById that checks if the element I have is a child of the <div>...

      Thanks Dorm,

      -Frinny

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Hmm, not sure how the nodeType property is going to help me.

        I guess I should provide more details on what I'm trying to do.

        I am using ASP.NET UpdatePanels. The way it works in ASP.NET is that whatever's placed in an UpdatePanel takes part in Ajax/asynchronous requests to the server. When the request returns it's stripped down to only return the data required to refresh that section on the page (UpdatePanels are rendered as <div>s so it only refreshes the content that is in the <div> that is the UpdatePanel).

        I have an element that needs to be refreshed only if it partakes in one of the Ajax requests...only if it's in an active UpdatePanel.

        But right now whenever any ajax request comes back to the browser the code to update the element is executed.

        I need a way to only execute this code if the element is in the "active" UpdatePanel and needs to be updated.

        I have the ID of the element that needs to be updated...and I have access to a list of all of the UpdatePanel IDs that were involved in the Ajax request.

        I just need to figure out if the element is a child of one of the UpdatePanels in the list so that I can call the method that updates the element.

        I've tried a whole bunch of things and now I'm starting think about looping through all of the child elements in the list of UpdatePanels to determine if it's in there....

        I know there has to be a better way though!

        -Frinny

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Originally posted by Frinavale
          I am using ASP.NET UpdatePanels.
          argh, asp.net, my blind spot… *gg*

          Originally posted by Frinavale
          I need a way to only execute this code if the element is in the "active" UpdatePanel and needs to be updated.

          I have the ID of the element that needs to be updated...and I have access to a list of all of the UpdatePanel IDs that were involved in the Ajax request
          that ID is probably the Ajax requestId? what kind of list is that UpdatePanel ID list (in terms of DOM/JS)?

          Originally posted by Frinavale
          I just need to figure out if the element is a child of one of the UpdatePanels in the list so that I can call the method that updates the element.
          it all depends on the list type, at least you can search for the ID in every member (provided it is an element) with element.getElem entById(…)

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Oh they are "clientIDs" not the Ajax requestId.

            They are the the IDs of div elements that are the UpdatePanels.
            I can use the document.getEle mentById to retrieve the DOM element.

            The list of IDs is a JavaScript Array of ClientIDs :)

            I guess I'll just loop through the child IDs :)

            Thanks again,

            -Frinny

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              How "deep" nested do you need?
              You could keep checking the .parent of your javascript node until it made it's way up to document.body to see if your div was in it.
              Supposing you have an object and you want to see if it a child of a DIV:
              [code=javascript]
              function isChildOf(myobj ,ContainerObj)
              {
              var retval=false;
              var curobj;
              curobj=myobj.pa rent;
              while(curobj!=u ndefined)
              {
              if(myobj.parent ==document.body )
              { break;}
              //check to see if the curobj is the DIV container you are after
              // maybe just check the .id property for comparison?
              //if it is, set retval=true and break out of the loop?
              curobj=curobj.p arent;//move up the hierarchy
              }
              return retval;
              }
              [/code]

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                I'd rather go down than up...I have the IDs of the UpdatePanels I just need to check to see if the element is in one of them.

                I'm going to try the IndexOf method that's part of the Array class tomorrow :)

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  Down is a lot bulkier then up

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    Originally posted by Plater
                    Down is a lot bulkier then up
                    true, but I don’t know any built-in methods, that search upwards.

                    Comment

                    • Plater
                      Recognized Expert Expert
                      • Apr 2007
                      • 7872

                      #11
                      I suppose if there is a built in down method that searches through children of children, it makes for shorter code. Just seems awfully wasteful to search through siblings and their children and etc. Up produces the shortest path.

                      What about using XPath? Would the XPath of the child object be able to show the IDs of its container elements?
                      Or if you were look were looking to see if "MyObj" is a child of "MyContaine r", you could request all the objects with attribute ID="MyObj" who have a parent object with attribute ID="MyContainer " ? Any result means yes, no result means not in the container?

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        Originally posted by Plater
                        What about using XPath? Would the XPath of the child object be able to show the IDs of its container elements?
                        it would, although you need an XPath implementation for that.

                        Comment

                        • Frinavale
                          Recognized Expert Expert
                          • Oct 2006
                          • 9749

                          #13
                          Dormi, there is no indexOf method for the Array class.

                          I've never used XPath before. My XML skills are pretty basic. I researching the topic now.

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            Originally posted by Frinavale
                            Dorm, there is no indexOf method for the Array class.
                            Frinny, then try this indexOf() method of this Array class.

                            Comment

                            • Frinavale
                              Recognized Expert Expert
                              • Oct 2006
                              • 9749

                              #15
                              I'm using the getElementsByTa gName() method to retrieve the child elements in the UpdatePanel-div, but apparently this doesn't return an array.

                              It returns a "collection " of elements (I'm assuming a NodeList)...so apparently I can't use the array.indexOf() method to check if the element exists in the UpdatePanel-div.

                              Comment

                              Working...