Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Newbie Question on getElementsByTagName

    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:

    PHP Code:
    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=0links.lengthi++ ){
            
    links[i].onclick = function(){
              return 
    Add_This_Metal(this);
            };
          }
        }
      }



    Here is the html:

    PHP Code:
    <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

  • #2
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,302
    Thanks
    28
    Thanked 276 Times in 270 Posts
    Quote Originally Posted by Vik_R View Post
    What am I missing?
    Metals_user_may_want_to_add does not case‐sensitively match metals_user_may_want_to_add.

    Quote Originally Posted by Vik_R View Post
    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”.
    Last edited by Arbitrator; 08-21-2007 at 06:18 AM. Reason: I added a tangential comment.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #3
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •