how to code NodeList.getElementsByTagName  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| |
I’m trying to do the following - document.getElementsByTagName("sup").getElementsByTagName("a");
currently I have this because I somehow need to return a combined result NodeList - // can’t prototype into NodeList because of Firefox
-
if (!Object.getElementsByTagName && Object.length)
-
{
-
Object.prototype.getElementsByTagName = function(name)
-
{
-
// the only idea I got
-
var div = document.createElement("div");
-
for (var i=0, len=this.length >>> 0; i<len; i++)
-
{
-
if (i in this)
-
{
-
div.appendChild(this[i]); // line 92
-
}
-
}
-
return div.getElementsByTagName(name);
-
}
-
}
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 - <h4>15<sup>th</sup> – 24<sup>th</sup> Hesinde<sup><a href="#n1-3">3</a></sup></h4>
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: how to code NodeList.getElementsByTagName
I can prevent the Exception by - div.appendChild(this[i].cloneNode(true));
but that’s not returning the NodeList I need later …
|  | Needs Regular Fix | | Join Date: Mar 2008 Location: Chennai - India
Posts: 348
| | | re: how to code NodeList.getElementsByTagName
Hi Dormilich,
Do u want to get the anchor object inside the <sup>? I tried out this one... -
function callJS(){
-
var subObj = document.getElementsByTagName('sup');
-
for(i=0;i<subObj.length;i++)
-
{
-
try
-
{
-
var aObj = subObj[i].getElementsByTagName('a');
-
if(aObj.length>0)
-
{
-
alert("I got the Object at the Index "+i);
-
}
-
}
-
catch(e)
-
{}
-
}
-
}
Thanks and Regards
Ramanan Kalirajan
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: how to code NodeList.getElementsByTagName Quote:
Originally Posted by RamananKalirajan 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) …
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North
Posts: 5,066
| | | re: how to code NodeList.getElementsByTagName
Sorry, but what's the difference between "one list" and an array?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | 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…
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North
Posts: 5,066
| | | re: how to code NodeList.getElementsByTagName
Could the NamedNodeMap help you at all??
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North
Posts: 5,066
| | | re: how to code NodeList.getElementsByTagName
I think the NodeList is immutable (see this article...)...
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: how to code NodeList.getElementsByTagName
that’s also the impression I got…
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North
Posts: 5,066
| | | 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 -
<html>
-
<head>
-
</head>
-
<body>
-
<input type="text" id="text1" name="text1" />
-
<input type="text" id="text2" name="text2" />
-
<input type="text" id="text3" name="text3" />
-
-
<select id="select1" name="select1">
-
<option>1</option>
-
<option>2</option>
-
</select>
-
<select id="select2" name="select2">
-
<option>1</option>
-
<option>2</option>
-
</select>
-
-
<script type="text/javascript">
-
-
var inputs = document.getElementsByTagName('input');
-
var selects = document.getElementsByTagName('select');
-
-
-
var all = new Array(inputs.length + selects.length);
-
var index = 0;
-
for (i = 0; i < inputs.length; i++)
-
all[index++] = inputs[i];
-
for (i = 0; i < selects.length; i++)
-
all[index++] = selects[i];
-
-
-
</script>
-
-
</body>
-
</html>
Sorry...maybe someone else knows more :)
I'm pretty new to JavaScript.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: how to code NodeList.getElementsByTagName Quote:
Originally Posted by Frinavale I'm pretty new to JavaScript. for being new you’re quite good :)
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North
Posts: 5,066
| | | 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).
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: how to code NodeList.getElementsByTagName Quote:
Originally Posted by Frinavale (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.
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North
Posts: 5,066
| | | re: how to code NodeList.getElementsByTagName Quote:
Originally Posted by Dormilich 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?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: how to code NodeList.getElementsByTagName Quote:
Originally Posted by Frinavale 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…
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | 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?
|  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|