...

View Full Version : Newbie Question on getElementsByTagName



Vik_R
08-21-2007, 03:29 AM
I have a newbie question on getElementsByTagName. Here's some Javascript I'm working on.

It's finding the unordered list - when I step through it it with the FireBug plugin for FireFox, after the call to getElementById, I see the ul Metals_from_search, with 3 childnodes.

But after the call to getElementsByTagName, the links variable is shown with a length of zero.

What am I missing?

Thanks very much in advance to all for any info.


-Vik

Here's the Javascript:


function PrepareMetalsToBeAddedToListOfMetalsUserLikes(){
if( document.getElementById &&
document.getElementsByTagName ){
if( document.getElementById( 'Metals_user_may_want_to_add' ) ){
var Metals_from_search = document.getElementById( 'Metals_user_may_want_to_add' );
var links = Metals_from_search.getElementsByTagName( "a" );
for( var i=0; i < links.length; i++ ){
links[i].onclick = function(){
return Add_This_Metal(this);
};
}
}
}
}



Here is the html:


<ul id="metals_user_may_want_to_add">
<li>
<a href="http://localhost:8888/index.php/Add_metals/add_this_metal/9148" title = "Click to add this to the list of metals you like">
Copper</a>
</li>
<li>
<a href="http://localhost:8888/index.php/Add_metals/add_this_metal/9405" title = "Click to add this to the list of metals you like">
Iron</a>
</li>
<li>
<a href="http://localhost:8888/index.php/Add_metals/add_this_metal/14123" title = "Click to add this to the list of metals you like">
Gold</a>
</li>
</ul>

Arbitrator
08-21-2007, 06:13 AM
What am I missing? Metals_user_may_want_to_add does not case‐sensitively match metals_user_may_want_to_add.


It's finding the unordered list - when I step through it it with the FireBug plugin for FireFox, after the call to getElementById, I see the ul Metals_from_search, with 3 childnodes.Since we’re talking case‐sensitivity here, note that it’s “Firebug” and “Firefox”, not “FireBug” and “FireFox”.

Vik_R
08-21-2007, 09:19 AM
Thanks for pointing out these errors, Arbitrator. I corrected the case inconsistencies, but I still am getting the same anomaly. The code finds the unordered lists, and the three items it contains, but does not yet associate an onclick function with them. What can I look into in order to resolve this?

Vik_R
08-21-2007, 04:28 PM
Fixed!

Let me try to explain the newbie error I made. :)

The function that built the list of metals, was being called twice - first, two generate an autocomplete list for the text input box - and after that, (i.e. after the user clicks the submit button), to generate a list of the metals that the user could click on to add a specific metal. This function had a parameter ($addlinks) that could be set to "y" to let it generate the links code, or to "n", in which case, it didn't generate the links.

I _thought_ that the first list of metals was gone by the time the Javascript was called - but it was still available to the Javascript in the DOM.

So all I had to do, was make sure that the id of the linked list isn't set to "Metals_user_may_want_to_add", unless $addlinks is set to "y".

The Javascript now finds the links!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum