473,618 Members | 3,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to code NodeList.getEle mentsByTagName

Dormilich
8,658 Recognized Expert Moderator Expert
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_INVAL ID_POINTER)" nsresult: "0x80004003 (NS_ERROR_INVAL ID_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
15 5764
Dormilich
8,658 Recognized Expert Moderator Expert
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 Contributor
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 Recognized Expert Moderator Expert
@RamananKaliraja n
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 Recognized Expert Moderator Expert
Sorry, but what's the difference between "one list" and an array?
Sep 8 '09 #5
Dormilich
8,658 Recognized Expert Moderator Expert
RamananKaliraja n’s code doesn’t have a return value (aka one list)

getElementsByTa gName() usually returns a NodeList. if I prototype getElementsByTa gName 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 Recognized Expert Moderator Expert
Could the NamedNodeMap help you at all??
Sep 8 '09 #7
Frinavale
9,735 Recognized Expert Moderator Expert
I think the NodeList is immutable (see this article...)...
Sep 8 '09 #8
Dormilich
8,658 Recognized Expert Moderator Expert
that’s also the impression I got…
Sep 8 '09 #9
Frinavale
9,735 Recognized Expert Moderator Expert
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

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

Similar topics

1
2238
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 when it is dloaded and I open it, it contains te HTML code of the file displayed... Wordt thin g is - it _used_ to work so the code is OK - only some little thing changed. Maybe the header part??? Thanks.
2
2766
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 that refers to them. I suppose I can imagine cases where this idea would be useful, but I can imagine many more where I'd like to get a *static* list of, e.g., the <p> elements in my document, and do something with them, regardless of any I...
8
4235
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: I load xml via httpwebrequest etc and put it in a XmlDocument. xmlFeed.Load(xRes.GetResponseStream()); This works fine as i can use this stream with a XmlReader to parse the
1
1213
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 XmlDocument.GetElementsByName(). Goofy things happen. I get back 8 elements, but as I step through the nodes, encrypting and calling EncryptedXml.ReplaseData(), I end up with several of the elements turning into null. I started by treating the nodelist as an array, and...
3
1834
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.) Request an Async request of the XML 2.) Once complete, parse the appropriate XML data to parse
1
2000
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 a nodelist, iterating through it and getting stopped after the first document.write, which I think is because the nodelist reference disappears after the document is changed with document.write. javascript: var someElements =...
2
5062
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 every case except if the pNd is the NodeList at which point I end up with the nodeList in the first element of the array instead of acting like the array would. function doSomething(pNd){ if(pNd.constructor != Array){pNd = new Array(pNd)}...
2
2175
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, "__len__") returns NULL. i naively added __len__ = _get_length
1
1330
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 file = new File("c://adv.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder();
0
8212
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8455
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7126
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6101
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5552
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4065
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2587
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.