Javascript and DOM hierarchy
If I have <element1> which is somewhere within my XHTML document, and element one has a shit-ton of child nodes, and I call:
var children = element1.getElementsByTagName('a');
Then children will be an array containing element pointers/objects for all of the 'a' tags within <element1>, but nowhere else on the page. Right?
muppet
September 7th, 2006 12:31pm
Mat's not there to tell you to read the specs... :)
Senthilnathan N.S.
September 7th, 2006 12:33pm
muppet
September 7th, 2006 12:35pm
Yeah, seriously, isn't there a manual for this.
The answer is yes. It's limited to children of that element.
Wayne (AHA)
September 7th, 2006 12:36pm
I think it's better if you have check if the length is greater than 0. I've had runtime errors because of not doing it.
Senthilnathan N.S.
September 7th, 2006 12:44pm
OK so why when I run this:
var links = document.getElementById('lithCore_ThreadList').getElementsByTagName('a');
Am I only getting the first anchor element, and none of the subsequent ones? 'lithCore_Threadlist' contains one hundred anchor tags.
muppet
September 7th, 2006 1:08pm
the reason I say I'm only getting the first element is that when I run this:
for (i = 0;i < links.length; i++) {
alert(links[i].id);
}
I only get one alert box, with the ID of the first anchor tag.
muppet
September 7th, 2006 1:09pm
I sort of remember having such an issue but I can't recollect right now to what was causing it. Did you try giving it in two separate statements the way it's given in the above link?
Senthilnathan N.S.
September 7th, 2006 1:27pm
When I do this:
var threadDiv = document.getElementById('lithCore_ThreadList');
var links = threadDiv.getElementsByTagName('a');
I get NONE of the anchor tags in lithCore_ThreadList
Clearly I'm doing something wrong, but what?!
muppet
September 7th, 2006 1:35pm
Do the anchor tags have to be DIRECT descendants to show up?
muppet
September 7th, 2006 1:46pm
based on this from mozilla, I'd guess 'no':
var table = document.getElementById("forecast-table");
var cells = table.getElementsByTagName("td");
muppet
September 7th, 2006 1:46pm
Are you sure you're getting the ID for the *first* link and not some other link? I know it sounds like a stupid question, but you never know.
Wayne (AHA)
September 7th, 2006 1:52pm
Right now I'm not getting ANY ids.
muppet
September 7th, 2006 1:53pm
What did you break?
Wayne (AHA)
September 7th, 2006 1:56pm
I switched my original line with chained method calls to two lines.
muppet
September 7th, 2006 1:58pm
I'm getting the links.length as 100 here. I'm using IE. If it's FF you are using I think we should check the differences for the browsers.
Senthilnathan N.S.
September 7th, 2006 1:58pm
Original:
var links = document.getElementById('lithCore_ThreadList').getElementsByTagName('a');
New:
var threadDiv = document.getElementById('lithCore_ThreadList');
var links = threadDiv.getElementsByTagName('a');
First one gives me some ID, which I assumed was the first <a> tag.
Second one gives me bupkis.
muppet
September 7th, 2006 1:59pm
SNS -
Weird, man. Maybe it's my work proxy foiling me.
I took the code from mozilla.org, so you'd think it'd be fine for FF.
muppet
September 7th, 2006 2:00pm
"First one gives me some ID, which I assumed was the first <a> tag."
Find out for sure... if it's not the first ID then I have a theory. If it is, then I don't. ;)
Wayne (AHA)
September 7th, 2006 2:01pm
Can't be my work proxy, the javascript isn't getting rewritten at all.
I'm stumped.
I added an alert for links.length. I downloaded FF's Javascript debugger but the interface has me flummoxed.
muppet
September 7th, 2006 2:02pm
I get a msgbox that says 100 in Firefox as well.
Wayne (AHA)
September 7th, 2006 2:02pm
Also, the 'onLoad' event on the footer div seems never to fire. But that's another issue. :-)
muppet
September 7th, 2006 2:02pm
I got '100' in my msgbox just now.
Ok the problem is elsewhere.
Garrrrrr.
muppet
September 7th, 2006 2:04pm
Earlier I had the alert in the loop that iterates through 'links', and I only ever got ONE msgbox.
muppet
September 7th, 2006 2:05pm
Like right now.
The loop only seems to iterate once.
muppet
September 7th, 2006 2:06pm
The ID that comes up IS the id of the first <a> tag.
muppet
September 7th, 2006 2:07pm
I think I know why...
muppet
September 7th, 2006 2:08pm
Nope, I don't.
Fuck.
muppet
September 7th, 2006 2:09pm
Mup, although this doesn't help you with your actual problem (which I'm too tired to think about at the moment) it's probably worth modifying your code to make sure it IS looking at the right links -- basing anything on the assumption that what works "by chance" now will always work is the way to an early grave. :)
Think I've got it.
muppet
September 7th, 2006 2:10pm
The links.length gets displayed fine.
alert(links[i].id);
var index = links[i].id;
updatedIDs[index] = true;
if (messageIDs[index] != true) {
newMessages.push(index);
}
The above code displays the alert only once and it is the ID of the first 'a' tag.
alert(links[i].id);
/*var index = links[i].id;
updatedIDs[index] = true;
if (messageIDs[index] != true) {
newMessages.push(index);
}*/
After commenting the way above I get the subsequent alerts and they too are the correct IDs.
Senthilnathan N.S.
September 7th, 2006 2:11pm
Yeah, the trouble was that I need to initialize variables as Arrays explicitly in javascript, and I didn't know that.
Every other script language I use has autovivification. :-)
muppet
September 7th, 2006 2:12pm
Thanks for your help. Seriously.
muppet
September 7th, 2006 2:15pm
Most welcome.
I was bored and it was good diversion to think about this...
Senthilnathan N.S.
September 7th, 2006 2:26pm