473,383 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

how to code NodeList.getElementsByTagName

Dormilich
8,658 Expert Mod 8TB
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:
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>
Sep 8 '09 #1

✓ answered by Dormilich

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?

15 5740
Dormilich
8,658 Expert Mod 8TB
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 …
Sep 8 '09 #2
RamananKalirajan
608 512MB
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
Sep 8 '09 #3
Dormilich
8,658 Expert Mod 8TB
@RamananKalirajan
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) …
Sep 8 '09 #4
Frinavale
9,735 Expert Mod 8TB
Sorry, but what's the difference between "one list" and an array?
Sep 8 '09 #5
Dormilich
8,658 Expert Mod 8TB
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…
Sep 8 '09 #6
Frinavale
9,735 Expert Mod 8TB
Could the NamedNodeMap help you at all??
Sep 8 '09 #7
Frinavale
9,735 Expert Mod 8TB
I think the NodeList is immutable (see this article...)...
Sep 8 '09 #8
Dormilich
8,658 Expert Mod 8TB
that’s also the impression I got…
Sep 8 '09 #9
Frinavale
9,735 Expert Mod 8TB
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.
Sep 8 '09 #10
Dormilich
8,658 Expert Mod 8TB
@Frinavale
for being new you’re quite good :)
Sep 8 '09 #11
Frinavale
9,735 Expert Mod 8TB
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).
Sep 8 '09 #12
Dormilich
8,658 Expert Mod 8TB
@Frinavale
that’s the point, if everyone (and I mean everyone) would stick to the specs, life would be a lot easier.
Sep 8 '09 #13
Frinavale
9,735 Expert Mod 8TB
@Dormilich
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?
Sep 8 '09 #14
Dormilich
8,658 Expert Mod 8TB
@Frinavale
that’s part of the development cycle…
Sep 8 '09 #15
Dormilich
8,658 Expert Mod 8TB
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?
Sep 9 '09 #16

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

Similar topics

1
by: Marc | last post by:
Hello I have a website where I can upload a file via a form. The uploaded file is stored in a MySQL database. When dloading te file I get a save as... dialog box, letting me save the file. But...
2
by: Howard Jess | last post by:
Given the html at the end of this message, I see how a DOM NodeList exhibits its "live" behavior; that is, adding elements to a document can change any NodeList variables, when there's *no* code...
8
by: Øyvind Jægtnes | last post by:
I'm playing around a bit with XPath and nodelist and i want to extract some info from a RSS feed. The one that i am testing at can be viewed at http://slashdot.org/index.rss Ok.. heres the deal:...
1
by: daniel | last post by:
Hi, Ignorant .Net Newbie Here - I'm loading up an XML document and trying to encrypt some elements but not others, by iterating through the nodelist returned from...
3
by: christopher.davidson | last post by:
Hello, I am working with XML files and utilizing Array functions to take the XML data and combined it with some html code to display a particular page. The process currently works like so: ...
1
by: DP413 | last post by:
Hey guys need your help :) I'm trying to clone a nodelist in a bookmarklet so that I can use document.write to output each item in the nodelist to the browser. Right now I'm basically grabbing...
2
by: carlback | last post by:
I have a function were the input parameter can either be an string,array,dom node or NodeList (getElementsByTagName()) and I have somthing like this function which works great for what I want in...
2
by: sndive | last post by:
i extract class object from an instance of NodeList (minicompat.py) like so PyObject *pclass = PyObject_GetAttrString( nodelistinsance, "__class__"); but PyObject_GetAttrString(pclass,...
1
by: ranjitha55 | last post by:
but it is displaying al values der in xml file.it should avoid reading duplicate values ,pls help me.. my code is, public class nm { public static void main(String argv) { try { File...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.