By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,261 Members | 1,674 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,261 IT Pros & Developers. It's quick & easy.

Check if element is child of another element

Frinavale
Expert Mod 5K+
P: 9,731
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
Sep 21 '09 #1
Share this Question
Share on Google+
35 Replies


Dormilich
Expert Mod 5K+
P: 8,639
just check out the nodeType property (it’s 3 for elements)

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

if an element is a child of another element
an element is always a child of another element (except the <html> element)
Sep 21 '09 #2

Frinavale
Expert Mod 5K+
P: 9,731
I'll check out the nodeType property....didn't see it listed as a property of the <div> element though.

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

Thanks Dorm,

-Frinny
Sep 21 '09 #3

Frinavale
Expert Mod 5K+
P: 9,731
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
Sep 21 '09 #4

Dormilich
Expert Mod 5K+
P: 8,639
@Frinavale
argh, asp.net, my blind spot… *gg*

@Frinavale
that ID is probably the Ajax requestId? what kind of list is that UpdatePanel ID list (in terms of DOM/JS)?

@Frinavale
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.getElementById(…)
Sep 21 '09 #5

Frinavale
Expert Mod 5K+
P: 9,731
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.getElementById 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
Sep 21 '09 #6

Plater
Expert 5K+
P: 7,872
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:
Expand|Select|Wrap|Line Numbers
  1. function isChildOf(myobj,ContainerObj)
  2. {
  3. var retval=false;
  4. var curobj;
  5. curobj=myobj.parent;
  6. while(curobj!=undefined)
  7. {
  8.    if(myobj.parent==document.body)
  9.    { break;}
  10.    //check to see if the curobj is the DIV container you are after
  11.    // maybe just check the .id property for comparison?
  12.    //if it is, set retval=true and break out of the loop?
  13.    curobj=curobj.parent;//move up the hierarchy
  14. }
  15. return retval;
  16. }
  17.  
Sep 21 '09 #7

Frinavale
Expert Mod 5K+
P: 9,731
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 :)
Sep 21 '09 #8

Plater
Expert 5K+
P: 7,872
Down is a lot bulkier then up
Sep 22 '09 #9

Dormilich
Expert Mod 5K+
P: 8,639
@Plater
true, but I don’t know any built-in methods, that search upwards.
Sep 22 '09 #10

Plater
Expert 5K+
P: 7,872
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 "MyContainer", 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?
Sep 22 '09 #11

Dormilich
Expert Mod 5K+
P: 8,639
@Plater
it would, although you need an XPath implementation for that.
Sep 22 '09 #12

Frinavale
Expert Mod 5K+
P: 9,731
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.
Sep 22 '09 #13

Dormilich
Expert Mod 5K+
P: 8,639

Frinavale
Expert Mod 5K+
P: 9,731
I'm using the getElementsByTagName() 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.
Sep 22 '09 #15

Dormilich
Expert Mod 5K+
P: 8,639
then just prototype it into the NodeList.
Sep 22 '09 #16

Frinavale
Expert Mod 5K+
P: 9,731
Give me a sec to try :)
Sep 22 '09 #17

Dormilich
Expert Mod 5K+
P: 8,639
for some reason the NodeList interface is write protected in Firefox, you need to use Object instead.
Sep 22 '09 #18

Frinavale
Expert Mod 5K+
P: 9,731
In order to convert the NodeList into an Array I'd have to loop through all of the nodes and add them to an array:
Expand|Select|Wrap|Line Numbers
  1. NodeList.prototype.toArray = function()
  2. {
  3.     var arr = new Array();
  4.         var  len = this.length;
  5.         var i;
  6.     for(i=0; i < len; i++)
  7.     {
  8.         arr.push(this[i]);
  9.     }
  10.     return arr;
  11. };
Since I'll end up looping through the nodes anyways, I might as well just leave my code the way it is (looping through all of the child nodes in the node list to see if the ID matches the element I'm looking for).

I don't see any benefit to doing this, it's actually worse because I have a break statement in my code to stop looping once I've found the element...unless you can explain one to me?
Sep 22 '09 #19

Dormilich
Expert Mod 5K+
P: 8,639
the benefit built upon having an array of objects, so that you could use the internal Array functions. if you have a NodeList you can only make the code easier to handle (you have to code the loop only once otherwise every time you want to use it)
Sep 22 '09 #20

Frinavale
Expert Mod 5K+
P: 9,731
Ok, I see the benefit :)

But it's not beneficial in my case.

I only have to use the loop once.... when an ajax request ends and returns to the browser I just want to make sure that the function called to "reset/initialize" my element isn't called if it isn't necessary. I don't need to use the array (or NodeList) after that point.
Sep 22 '09 #21

Plater
Expert 5K+
P: 7,872
My earlier javascript was incorrect. I corrected it (in case someone else wants an upwards search)
I also made it take either the ID or the object itself. Its verbose and could be greatly condensed
Expand|Select|Wrap|Line Numbers
  1. function isChildOf(ChildObject,ContainerObject) 
  2.     var retval=false; 
  3.     var curobj; 
  4.     if(typeof(ContainerObject)=="string")
  5.     {
  6.         ContainerObject=document.getElementById(ContainerObject);
  7.     }
  8.     if(typeof(ChildObject)=="string")
  9.     {
  10.         ChildObject=document.getElementById(ChildObject);
  11.     }
  12.     curobj=ChildObject.parentNode; 
  13.     while(curobj!=undefined) 
  14.     { 
  15.         if(curobj==document.body) 
  16.         { break;} 
  17.         if(curobj.id==ContainerObject.id)
  18.         {
  19.             retval =true;
  20.             break;
  21.         }
  22.         curobj=curobj.parentNode;//move up the hierarchy 
  23.     } 
  24.     return retval; 
  25.  
Sep 22 '09 #22

Frinavale
Expert Mod 5K+
P: 9,731
Thanks Plater :)

Looking at your code I see that you move all the way up to the document object in the case when the element is not a child of the ContainerObject. This means that your code will end up continuing to loop through all of the elements that the ContainerObject is nested within.

The following counts the number of iterations it would take to execute your code if the ContainerObject was nested within another element:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.  
  4. <script type="text/javascript">
  5. function isChildOf(ChildObject,ContainerObject) 
  6. {   var iterations = 0;
  7.     var retval=false; 
  8.     var curobj; 
  9.     if(typeof(ContainerObject)=="string")
  10.     {
  11.         ContainerObject=document.getElementById(ContainerObject);
  12.     }
  13.     if(typeof(ChildObject)=="string")
  14.     {
  15.         ChildObject=document.getElementById(ChildObject);
  16.     }
  17.     curobj=ChildObject.parentNode; 
  18.     while(curobj!=undefined) 
  19.     {   iterations++;
  20.         if(curobj==document.body) 
  21.         { 
  22.             document.write("<br />document.body reached while searching " + ContainerObject.id +" for " + ChildObject.id +". <br />");
  23.             break;
  24.         } 
  25.         if(curobj.id==ContainerObject.id)
  26.         {
  27.             retval =true;
  28.             break;
  29.         }
  30.         curobj=curobj.parentNode;//move up the hierarchy 
  31.     } 
  32.     document.write("It took " + iterations + " iterations to determine that ");
  33.     return retval; 
  34. </script>
  35.  
  36.  <div id="someDiv">
  37.   <div id="theUpdatePanel">
  38.     <div id="theChildElement"></div>
  39.   </div>
  40.   <div id="notTheChildElement"></div>
  41.  </div>
  42. <script type="text/javascript">
  43.  
  44. if(isChildOf("theChildElement","theUpdatePanel")){
  45.   document.write("theChildElement is a child of theUpdatePanel <br />");
  46. }else{
  47.   document.write("theChildeElement is NOT a child of theUpdatePanel <br />");
  48. }
  49.  
  50. if(isChildOf("notTheChildElement","theUpdatePanel")){
  51.   document.write("notTheChildElement is a child of theUpdatePanel <br />");
  52. }else{
  53.    document.write("notTheChildElement is NOT a child of theUpdatePanel <br />");
  54. }
  55.  
  56. </script>
  57.  
  58. </body>
  59. </html>
  60.  
It seems like a waste of resources to search all of the elements that the ContainerObject is nested in just to determine if the ChildObject exists within the ContainerObject. The ContainerObject could be nested pretty deep.

We know that the childObject should exist within the ContainerObject in order for the function to evaluate to true.

Why would I want to search up through the parent elements.... elements that don't need to be checked to determine if the childObject is a child of the ContainerObject?

I modified your code to search down through the childNodes in the ContainerObject.

It takes less iterations to determine if the childObject is a child of the ContainerObject by searching down if the ContainerObject is nested in another element because it is not looping through the parent elements of the ContainerObject:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.  
  4. <script type="text/javascript">
  5. function isChildOf(ChildObject,ContainerObject) 
  6. {   var iterations = 0;
  7.     var retval=false; 
  8.     var curobj; 
  9.     if(typeof(ContainerObject)=="string")
  10.     {
  11.         ContainerObject=document.getElementById(ContainerObject);
  12.     }
  13.     if(typeof(ChildObject)=="string")
  14.     {
  15.         ChildObject=document.getElementById(ChildObject);
  16.     }
  17.  
  18.     var childNodes  = ContainerObject.getElementsByTagName('*');
  19.  
  20.     for(var i=0;i<childNodes.length;i++)  
  21.     {   var curobj = childNodes[i]; //move down
  22.         iterations++;
  23.         if(curobj==ChildObject)
  24.         {
  25.             retval =true;
  26.             break;
  27.         } 
  28.     } 
  29.     document.write("It took " + iterations + " iterations to find out ");
  30.     return retval; 
  31. </script>
  32.  
  33.  
  34.  <div id="someDiv">
  35.   <div id="theUpdatePanel">
  36.     <div id="theChildElement"></div>
  37.   </div>
  38.   <div id="notTheChildElement"></div>
  39.  </div>
  40.  
  41. <script type="text/javascript">
  42.  
  43. if(isChildOf("theChildElement","theUpdatePanel")){
  44.   document.write("theChildElement is a child of theUpdatePanel <br />");
  45. }else{
  46.   document.write("theChildElement is NOT a child of theUpdatePanel <br />");
  47. }
  48.  
  49. if(isChildOf("notTheChildElement","theUpdatePanel")){
  50.   document.write("notTheChildElement is a child of theUpdatePanel <br />");
  51. }else{
  52.    document.write("notTheChildElement is NOT a child of theUpdatePanel <br />");
  53. }
  54.  
  55. </script>
  56.  
  57. </body>
  58. </html>
-Frinny
Sep 22 '09 #23

Frinavale
Expert Mod 5K+
P: 9,731
Ok Plater, I think I know why you're suggesting an upwards search.....

I'm not sure if I'm searching properly downwards because I'm not checking the children of the children (unless the document.getElementsByTagName('*') returns everything?)

I'm going to have to investigate this more but right now I have to figure out yet another bug before I get back to this.

Sometimes I hate AJAX.NET
Sep 22 '09 #24

Frinavale
Expert Mod 5K+
P: 9,731
The document.getElementsByTagName('*') does return all of the children of the children... of the parent node.

I tested the two codes again but this time I added more depth to the ContainerNode...

Comparison testing code:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.  
  4. <script type="text/javascript">
  5. function isChildOfDown(ChildObject,ContainerObject) 
  6. {   var iterations = 0;
  7.     var retval=false; 
  8.     var curobj; 
  9.     if(typeof(ContainerObject)=="string")
  10.     {
  11.         ContainerObject=document.getElementById(ContainerObject);
  12.     }
  13.     if(typeof(ChildObject)=="string")
  14.     {
  15.         ChildObject=document.getElementById(ChildObject);
  16.     }
  17.  
  18.     var childNodes  = ContainerObject.getElementsByTagName('*');
  19.  
  20.     for(var i=0;i<childNodes.length;i++)  
  21.     {   var curobj = childNodes[i]; //move down
  22.         iterations++;
  23.         if(curobj==ChildObject)
  24.         {
  25.             retval =true;
  26.             break;
  27.         } 
  28.     } 
  29.     document.write("It took " + iterations + " iterations to find out ");
  30.     return retval; 
  31.  
  32. function isChildOfUp(ChildObject,ContainerObject) 
  33. {   var iterations = 0;
  34.     var retval=false; 
  35.     var curobj; 
  36.     if(typeof(ContainerObject)=="string")
  37.     {
  38.         ContainerObject=document.getElementById(ContainerObject);
  39.     }
  40.     if(typeof(ChildObject)=="string")
  41.     {
  42.         ChildObject=document.getElementById(ChildObject);
  43.     }
  44.     curobj=ChildObject.parentNode; 
  45.     while(curobj!=undefined) 
  46.     {   iterations++;
  47.         if(curobj==document.body) 
  48.         { 
  49.             break;
  50.         } 
  51.         if(curobj.id==ContainerObject.id)
  52.         {
  53.             retval =true;
  54.             break;
  55.         }
  56.         curobj=curobj.parentNode;//move up the hierarchy 
  57.     } 
  58.     document.write("It took " + iterations + " iterations to determine that ");
  59.     return retval; 
  60. </script>
  61. <div id="topDiv">
  62.   <div id="someUpperDiv">
  63.    <div id="someDiv">
  64.     <div id="theUpdatePanel">
  65.       <div id="anotherChild">
  66.         <div id="yetAnotherChild">
  67.           <div id="anotherLevel">
  68.            <div id='oneMore'>
  69.             <div id="theChildElement"></div>
  70.            </div>
  71.           </div>
  72.         </div>
  73.       </div>
  74.     </div>
  75.     <div id="anotherUpdatePanel">
  76.       <div id="notTheChildElement"></div>
  77.     </div>
  78.    </div>
  79.   </div>
  80. </div>
  81. <script type="text/javascript">
  82. document.write("<fieldset>");
  83. document.write("<legend>isChildOf Searching Up</legend>");
  84. document.write("Found: <br />");
  85. if(isChildOfUp("theChildElement","theUpdatePanel")){
  86.   document.write("theChildElement is a child of theUpdatePanel <br />");
  87. }else{
  88.   document.write("theChildeElement is NOT a child of theUpdatePanel <br />");
  89. }
  90. document.write("<br />Not found: <br />");
  91. if(isChildOfUp("notTheChildElement","theUpdatePanel")){
  92.   document.write("notTheChildElement is a child of theUpdatePanel <br />");
  93. }else{
  94.    document.write("notTheChildElement is NOT a child of theUpdatePanel <br />");
  95. }
  96. document.write("</fieldset><br />");
  97.  
  98. document.write("<fieldset><legend>isChildOf Searching Down</legend>")
  99. document.write("Found: <br />");
  100. if(isChildOfDown("theChildElement","theUpdatePanel")){
  101.   document.write("theChildElement is a child of theUpdatePanel <br />");
  102. }else{
  103.   document.write("theChildeElement is NOT a child of theUpdatePanel <br />");
  104. }
  105.  document.write("<br />Not found: <br />");
  106. if(isChildOfDown("notTheChildElement","theUpdatePanel")){
  107.   document.write("notTheChildElement is a child of theUpdatePanel <br />");
  108. }else{
  109.    document.write("notTheChildElement is NOT a child of theUpdatePanel <br />");
  110. }
  111. document.write("</fieldset>");
  112.  
  113. </script>
  114.  
  115. </body>
  116. </html>
  117.  
Right now they have the same iterations...so one could pretty much conclude that it doesn't matter which way you go.

But it would all depend on the depth that the child node is with respect to the ContainerNode and with respect to the document....

If the majority of the depth was in the Container node, then searching up might be the best choice.....

If the majority of the depth was in the rest of the document, then searching down would be the best choice...

-Frinny
Sep 22 '09 #25

Plater
Expert 5K+
P: 7,872
It seems like a waste of resources to search all of the elements that the ContainerObject is nested in just to determine if the ChildObject exists within the ContainerObject. The ContainerObject could be nested pretty deep.
Lol as opposed to searching all the siblings? You can have almost infinte siblings(and they can have infinte children). There is only one parentNode per object. Yes it could be nested down, but draw tree diagram and see what results in a faster path.

Here's a tree:



Now say you wanted to see if V was a child of C:
Going up: V->R->M->H->C (yes)
Going down (depending on depth vs breadth):
C->F->K->G->L->Q->S->W->X->Y->H->M->R->T->U->V (yes)

If you wanted to see if P was a child of C:
Going up: P->J->E->B->A (no)
Going down:
C->F->K->G->L->Q->S->W->X->Y->H->M->R->T->U->V->Z (no)


Now, in your case its roughly a one descendant thing right? Like it will be a direct child and not a child of a child right? In which case, going up will be a single hop, where as going down will be anywhere 1-n where n is the number of children in the parent.

As for searching all the way up to document.body, you could always make some other stopping point in the hierarchy.


Shortened javascript:
Expand|Select|Wrap|Line Numbers
  1. function isChildOf(ChildObject,ContainerObject) 
  2.     var curobj; 
  3.     if(typeof(ContainerObject)=="string")
  4.     {        ContainerObject=document.getElementById(ContainerObject);    }
  5.     if(typeof(ChildObject)=="string")
  6.     {        ChildObject=document.getElementById(ChildObject);    }
  7.     for(curobj=ChildObject.parentNode; ( (curobj!=undefined) && (curobj!=document.body) &&(curobj.id!=ContainerObject.id) );curobj=curobj.parentNode)
  8.     {    }
  9.     return (curobj.id==ContainerObject.id);
  10. }
  11.  
Sep 22 '09 #26

Frinavale
Expert Mod 5K+
P: 9,731
Ok the image makes things a lot more clear.

I've found myself to be a pretty linear thinker. I haven't had a lot of exposure to trees before..but I'm expanding my thought process to include thinking out side of the "linear" logical path :).... (it all started with those asynchronous sockets a few months ago)

The element is not a direct decedent.

It varies from page to page. The element is in a user control which can be used multiple times on a page and within other user controls. The element could be within a user control within a user control within a user control within a page...which is part of a master page.

I think the deepest I've used the control goes is down about 20 nodes deep (could be more). But within an UpdatePanel it's significantly less (half that at least).

I'm going about solving this problem completely wrong and right now I hate my design. This control was designed before I even knew what Ajax was and I barely had enough knowledge of JavaScript to get it to work.

I'm going to redesign the control that needs to be updated so that its an Ajax Enabled Server control instead of a user control. After I convert it everything will be much cleaner because I'll be using a JavaScript Object to manage when the element is updated...and I wont have to mess around with trying to figure out if it was in the active UpdatePanel etc.

Thanks a lot for taking the time to explain things to me!
I'm going to have to review my search methods and definitely review trees in my spare time.

-Frinny
Sep 22 '09 #27

P: 1
@Frinavale
not being hampered by having used asp.net before, coding issues are coding issues so the techique stays the same a bit. :) and not afraid to ask a stupid question, lol, so here goes...

I may be way off base here, have you considered using triggers?
Sep 26 '09 #28

Frinavale
Expert Mod 5K+
P: 9,731
That's not a stupid question.

Triggers (I'm assuming you're referring to UpdatePanel triggers) are used to specify that a particular control "triggers" either an asynchronous or full page request to the server in order to refresh the UpdatePanel that it's associated with.

My control didn't actually cause any postbacks to the server. It is a client side control that needed to maintain its state between postbacks to the server (whether it be asynchronous or not).

I developed this control as a "web user control" before I knew how to create Ajax-enabled server controls. I was hoping not to have to redesign it and to just use the PageRequestManager to determine whether or not the control needed to be "refreshed" in order to maintain it's state client side. (The PageRequestManger is a JavaScript object responsible for handling Ajax requests)

The problem with the PageRequestManager is that it's not specific to an UpdatePanel so it was very difficult to determine if my client side control needed to be "refreshed" in order to maintain it's state after a server postback. If the client side control was "refreshed" when it wasn't part of the Ajax request, then it was being incorrectly reset to the state that it was during the last request it was involved in.

I ended up recreating the control as a Ajax-enabled Server control. It is a lot cleaner now especially since I don't need to mess around with trying to figure out which UpdatePanel it belongs to with JavaScript during a postback to the server. I should have just redesigned it as soon as I knew that it needed to be modified to work with Ajax.

I'm happy with this new solution. It's working beautifully and it's going to be a lot easier to maintain in the future :)

-Frinny
Sep 28 '09 #29

Dormilich
Expert Mod 5K+
P: 8,639
a modification of Plater’s function
Expand|Select|Wrap|Line Numbers
  1. Element.prototype.isChildOf = function(ContainerObject) 
  2.     if ("string" == typeof ContainerObject)
  3.     {
  4.         ContainerObject = document.getElementById(ContainerObject);
  5.     }
  6.     if (!(ContainerObject instanceof Element))
  7.     {
  8.         throw new Error("Container Object is not an Element");
  9.     }
  10.  
  11.     var curobj = this;
  12.  
  13.     do
  14.     { 
  15.         curobj = curobj.parentNode;
  16.  
  17.         if (curobj == ContainerObject)
  18.         {
  19.             return true;
  20.         }
  21.     } 
  22.     while (curobj instanceof Element);
  23.     return false;
  24. }
Sep 30 '09 #30

Plater
Expert 5K+
P: 7,872
I took a few liberties with my for loop construction (abuse of how it works), but generally i've been told to use a for loop over a while loop. Something about faster coding or something.
Sep 30 '09 #31

P: 1
1) 'for' loops an 'do' loops are just Macro'd versions of while loops.
ie:
Expand|Select|Wrap|Line Numbers
  1. for ( var i = 0; i < length; i++)
gets translated to something like:
Expand|Select|Wrap|Line Numbers
  1. var i = 0; while(i < length && i++) { }
there is no performance difference between the two.

2) Why aren't you using the child's tag name in the search? If you call
Expand|Select|Wrap|Line Numbers
  1. parentNode.getElementsByTagName(childNode.tagName)
you'll take problem that could take n iterations and turning it into n/50 ( assuming there are 50, evenly distributed, types of nodes)

3) When someone takes the time to post sample code to try to help you, you DO NOT reply with more sample code analyzing the performance of the earlier sample code. That is the most ridiculous thing I've ever seen someone do on a programming forum.

4) When you say things like 'you can't', 'impossible', or 'the only way', 95% of the time, you're wrong. When you work at the big boys (MS, Google, IBM, etc), if you say something like that, you'll get 15 developers running at you (all with too much coffee in them) ready to tell you 5 reasons why you're wrong, and 10 reason why you should let them do the work for you.
Nov 11 '09 #32

Dormilich
Expert Mod 5K+
P: 8,639
@nmielnik
interesting point.

@nmielnik
well, this is an in-depth discussion thread, we know each other (to a certain extend), analyzing something is interesting and providing more understanding… do I need to list more reasons?

@nmielnik
either you work for those “big boys” or you’re in the wrong thread.

and another reason: didactics
Nov 11 '09 #33

Plater
Expert 5K+
P: 7,872
@Dormilich
Great, now I gotta go looking up words again.
Stupid $2 words :-D


@Plater
Hmm if a for loop is just a macro'd while loop, why does "while(true)" generate compiler warnings when "for(;;)" does not?
Nov 11 '09 #34

Frinavale
Expert Mod 5K+
P: 9,731
@nmielnik
I needed to check if the element was a child of the element that was actively partaking in an asynchronous request to the server. Upon returning I had no idea if the element was part of the request or not.

While your suggestion would reduce the amount of elements to search through, I was looking at this from a purely theoretical point of view and didn't want to get into the nitty-gritty details of my actual problem. I never consider it because this was a theoretical discussion.

My end solution didn't require looping at all. I used Objects that are aware of when they are taking part in an asynchronous request so that I didn't have to do any of this ugly looping/searching.


@nmielnik
Haha, I never thought of how confusing and weird that would look from an outsider.

I learned a lot from the conversation in this thread, and although some of it didn't exactly pertain to my original question it was very helpful in expanding my thinking outside of my regular, linear thinking.

@nmielnik
Hmm I'm not really sure what you're talking about here.
Are you referring to this:
@Frinavale
If so, you should probably research the NodeList JavaScript Object. There are a lot of limitations and restrictions on what you can and cannot do with this Object.

Sometimes certain approaches (such as using a NodeList in this scenario) are impossible and you need to come at it from another angle.

Nothing is impossible to achieve...but some approaches to trying to achieve what you want simply will not work no matter how much you want it to.

@Plater
Haha I had to look it up too. (wiki for didactic).

Funny how we are learning English vocabulary from someone who isn't a native English speaker.

-Frinny
Nov 11 '09 #35

Plater
Expert 5K+
P: 7,872
@nmielnik
Actually, peer-review and team development is considered one of the strongest forms of learning and accomplishing tasks. Dialog between members is of huge importance. That's why companies spend hundreds of thousands of dollars on team building exercises.
If you are one of those people who thinks its "rude" to have someone tweak your code/design, then engineering, software or otherwise is NOT for you.
Nov 12 '09 #36

Post your reply

Sign in to post your reply or Sign up for a free account.