Connecting Tech Pros Worldwide Help | Site Map

how to code NodeList.getElementsByTagName

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#1: Sep 8 '09
I’m trying to do the following
Expand|Select|Wrap|Line Numbers
  1. document.getElementsByTagName("sup").getElementsByTagName("a");
currently I have this because I somehow need to return a combined result NodeList
Expand|Select|Wrap|Line Numbers
  1. // can’t prototype into NodeList because of Firefox
  2. if (!Object.getElementsByTagName && Object.length)
  3. {
  4.     Object.prototype.getElementsByTagName = function(name)
  5.     {
  6.         // the only idea I got
  7.         var div = document.createElement("div");
  8.         for (var i=0, len=this.length >>> 0; i<len; i++)
  9.         {
  10.             if (i in this)
  11.             {
  12.                 div.appendChild(this[i]); // line 92
  13.             }
  14.         }
  15.         return div.getElementsByTagName(name);    
  16.     }
  17. }
but when running the code (FF 3.5.2) I get the following Exception:
Quote:
uncaught exception: [Exception... "Component returned failure code: 0x80004003 (NS_ERROR_INVALID_POINTER)" nsresult: "0x80004003 (NS_ERROR_INVALID_POINTER)" location: "JS frame :: http://localhost/phileasson.xhtml#n1-1 :: anonymous :: line 92" data: no]
there are some <sup> before that, which do work. by checking the created div I found that the mentioned node has been already copied …

can anyone help me in that matter?

HTML sample code
Expand|Select|Wrap|Line Numbers
  1. <h4>15<sup>th</sup> – 24<sup>th</sup> Hesinde<sup><a href="#n1-3">3</a></sup></h4>
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: Sep 8 '09

re: how to code NodeList.getElementsByTagName


I can prevent the Exception by
Expand|Select|Wrap|Line Numbers
  1. div.appendChild(this[i].cloneNode(true));
but that’s not returning the NodeList I need later …
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 348
#3: Sep 8 '09

re: how to code NodeList.getElementsByTagName


Hi Dormilich,
Do u want to get the anchor object inside the <sup>? I tried out this one...
Expand|Select|Wrap|Line Numbers
  1. function callJS(){
  2.         var subObj = document.getElementsByTagName('sup');
  3.         for(i=0;i<subObj.length;i++)
  4.         {
  5.             try
  6.             {
  7.                 var aObj = subObj[i].getElementsByTagName('a');
  8.                 if(aObj.length>0)
  9.                 {
  10.                     alert("I got the Object at the Index "+i);
  11.                 }
  12.             }
  13.             catch(e)
  14.             {}
  15.         }
  16.     }
Thanks and Regards
Ramanan Kalirajan
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#4: Sep 8 '09

re: how to code NodeList.getElementsByTagName


Quote:

Originally Posted by RamananKalirajan View Post

Do u want to get the anchor object inside the <sup>? I tried out this one...

yes, I want to get those anchors (which is not a problem as is), but I need them returned as one list (which is the problem here). an alert is of no use here :(

maybe it also works when returning an Array (though that’s not the same) …
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#5: Sep 8 '09

re: how to code NodeList.getElementsByTagName


Sorry, but what's the difference between "one list" and an array?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#6: Sep 8 '09

re: how to code NodeList.getElementsByTagName


RamananKalirajan’s code doesn’t have a return value (aka one list)

getElementsByTagName() usually returns a NodeList. if I prototype getElementsByTagName into the NodeList interface I’d like it to return a NodeList too (because that’s what you’d expect). if everything fails it could also return an array…
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#7: Sep 8 '09

re: how to code NodeList.getElementsByTagName


Could the NamedNodeMap help you at all??
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#8: Sep 8 '09

re: how to code NodeList.getElementsByTagName


I think the NodeList is immutable (see this article...)...
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#9: Sep 8 '09

re: how to code NodeList.getElementsByTagName


that’s also the impression I got…
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#10: Sep 8 '09

re: how to code NodeList.getElementsByTagName


I tried it...but it just wont work.
I don't think you can join NodeLists together.
Best I could do was create an array of nodes

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5. <input type="text" id="text1" name="text1" />
  6. <input type="text" id="text2" name="text2" />
  7. <input type="text" id="text3" name="text3" />
  8.  
  9. <select id="select1" name="select1"> 
  10. <option>1</option>
  11. <option>2</option>
  12. </select>
  13. <select id="select2" name="select2">
  14. <option>1</option>
  15. <option>2</option>
  16. </select>
  17.  
  18. <script type="text/javascript">
  19.  
  20. var inputs = document.getElementsByTagName('input');
  21. var selects = document.getElementsByTagName('select');
  22.  
  23.  
  24. var all = new Array(inputs.length + selects.length);
  25. var index = 0;
  26. for (i = 0; i < inputs.length; i++)
  27.     all[index++] = inputs[i];
  28. for (i = 0; i < selects.length; i++)
  29.     all[index++] = selects[i];
  30.  
  31.  
  32. </script>
  33.  
  34. </body>
  35. </html>
Sorry...maybe someone else knows more :)
I'm pretty new to JavaScript.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#11: Sep 8 '09

re: how to code NodeList.getElementsByTagName


Quote:

Originally Posted by Frinavale View Post

I'm pretty new to JavaScript.

for being new you’re quite good :)
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#12: Sep 8 '09

re: how to code NodeList.getElementsByTagName


Haha thanks Dorm :)

I sometimes think that JavaScript is going to make my head explode. I think that over half of my problems these days has something to do with JavaScript (and the messed up idea of what Microsoft thinks Ajax should be).
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#13: Sep 8 '09

re: how to code NodeList.getElementsByTagName


Quote:

Originally Posted by Frinavale View Post

(and the messed up idea of what Microsoft thinks Ajax should be).

that’s the point, if everyone (and I mean everyone) would stick to the specs, life would be a lot easier.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#14: Sep 8 '09

re: how to code NodeList.getElementsByTagName


Quote:

Originally Posted by Dormilich View Post

that’s the point, if everyone (and I mean everyone) would stick to the specs, life would be a lot easier.

Sometimes it's breaking the specs that advances technology though. So even though I would love for MS to follow the rules (because it would make my life a LOT easier), I think that we would be set back a lot by enforcing the rules on everyone...

How else are we supposed to know whether or not to change the specs (in order to advance technology) unless we try the bad with the good?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#15: Sep 8 '09

re: how to code NodeList.getElementsByTagName


Quote:

Originally Posted by Frinavale View Post

How else are we supposed to know whether or not to change the specs (in order to advance technology) unless we try the bad with the good?

that’s part of the development cycle…
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#16: Sep 9 '09

re: how to code NodeList.getElementsByTagName


ok, now that we figured out we can’t create NodeLists by ourselves, I’ll return to the original idea of putting the relevant elements to a common (new) parent and use that to apply getElementsByTagName(). I basicly need some sort of pointers, because the original plan didn’t work as expected, I can’t use cloned nodes and moving the nodes between document and ‘parent’ is very inconvenient.

any ideas in this direction?
Reply