Sanding our assholes with 150 grit. Slowly. Lovingly.

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?
Permalink Send private email muppet 
September 7th, 2006 12:31pm
Mat's not there to tell you to read the specs... :)
Permalink Send private email Senthilnathan N.S. 
September 7th, 2006 12:33pm
Yeah, seriously, isn't there a manual for this.

The answer is yes.  It's limited to children of that element.
Permalink Send private email 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.
Permalink Send private email 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.
Permalink Send private email 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.
Permalink Send private email 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?
Permalink Send private email 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?!
Permalink Send private email muppet 
September 7th, 2006 1:35pm
In case anybody cares, the relevant URL:

http://www.madebymonkeys.net/?template=lithCore_Index

The problem javascript is in the top frame, in getCurrentThreads().
Permalink Send private email muppet 
September 7th, 2006 1:43pm
Do the anchor tags have to be DIRECT descendants to show up?
Permalink Send private email 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");
Permalink Send private email 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.
Permalink Send private email Wayne (AHA) 
September 7th, 2006 1:52pm
Right now I'm not getting ANY ids.
Permalink Send private email muppet 
September 7th, 2006 1:53pm
What did you break?
Permalink Send private email Wayne (AHA) 
September 7th, 2006 1:56pm
I switched my original line with chained method calls to two lines.
Permalink Send private email 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.
Permalink Send private email 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.
Permalink Send private email 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.
Permalink Send private email 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.  ;)
Permalink Send private email 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.
Permalink Send private email muppet 
September 7th, 2006 2:02pm
I get a msgbox that says 100 in Firefox as well.
Permalink Send private email Wayne (AHA) 
September 7th, 2006 2:02pm
Also, the 'onLoad' event on the footer div seems never to fire.  But that's another issue.  :-)
Permalink Send private email muppet 
September 7th, 2006 2:02pm
I got '100' in my msgbox just now.

Ok the problem is elsewhere.

Garrrrrr.
Permalink Send private email 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.
Permalink Send private email muppet 
September 7th, 2006 2:05pm
Like right now.

The loop only seems to iterate once.
Permalink Send private email muppet 
September 7th, 2006 2:06pm
The ID that comes up IS the id of the first <a> tag.
Permalink Send private email muppet 
September 7th, 2006 2:07pm
I think I know why...
Permalink Send private email muppet 
September 7th, 2006 2:08pm
Nope, I don't.

Fuck.
Permalink Send private email 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. :)
Permalink Send private email Mat Hall 
September 7th, 2006 2:09pm
Think I've got it.
Permalink Send private email 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.
Permalink Send private email 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.  :-)
Permalink Send private email muppet 
September 7th, 2006 2:12pm
Thanks for your help.  Seriously.
Permalink Send private email muppet 
September 7th, 2006 2:15pm
Most welcome.

I was bored and it was good diversion to think about this...
Permalink Send private email Senthilnathan N.S. 
September 7th, 2006 2:26pm

This topic is archived. No further replies will be accepted.

Other topics: September, 2006 Other topics: September, 2006 Recent topics Recent topics