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 6 of 6
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    getElementsByTagName is not a function?

    Javascript/html noob here..any help greatly appreciated.

    Here's the deal, If I access the page directly on my pc via the file system (i.e. I open up the file via: file::/path../main.html, the page and script runs just fine.

    If however I point the browser to tomcat i.e. localhost:8080/blah
    firefox spits out the error: getElementsByTagName is not a function.

    Any thoughts or pointers? I'm totally confused over this..this is a built in function call?

    note: I did an instanceof on the object making the call, and it does indeed confirm it is an object.

    ps. I have confirmed I can access the all scripts/css files from the webserver (tomcat)

    System:
    FireFox (1.5.04), Tomcat, IE6 (also fails, even though I haven't found out how to see the js error details?)

  • #2
    New Coder
    Join Date
    Jan 2006
    Location
    Oslo, Norway
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A little code with the implemention would be useful to see what may be wrong. Have you for instance remembered to use document in front of the call? Like document.getElementsByTagName()
    Drømmejenta: Kristen jente som progger Python!

  • #3
    New Coder
    Join Date
    Aug 2005
    Location
    Edmonton, Canada
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It would help more to show a chunk of the sample code that where it is failing.

    The getElementsByTagName() method will only work on either the document object (document) or on html element nodes (p, a, div, etc...)
    PHP Code:
    /* returning a collection of elements from the document object */
    var pArray document.getElementsByTagName('p');

    /* using an element to return a collection of elements
        <p id="myP"><img ... /></p>
    */
    var imgArray document.getElementById('myP').getElementsByTagName('img'); 

  • #4
    New Coder
    Join Date
    Jun 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Resolved

    The code in question was:

    for (var k = 0; k < objs.length; k++)
    {
    var table = objs[k];
    alert(typeof table.getElementsByTagName("tbody"));
    var tbodies = table.getElementsByTagName("tbody");
    }

    On a hunch (I just recalled I hadn't fixed something prior on that page, pulled up another page using the same script..it worked)

    it turns out table while it was an 'object' I guess, was not defined, I added an:

    if (!table) check, and the js error went away, while I now know how to fix the problem, I'm still curious: why would:

    log.debug("table="+table); In the above code result in table=1 (log4j.js)
    I would have expected that it would have shown either undefined, or null??

    Thanx again all the quick responses.

  • #5
    New Coder
    Join Date
    Jun 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try replacing this line:

    Code:
    alert(typeof table.getElementsByTagName("tbody"));
    witht this:

    Code:
    alert(table);
    as Firefox will tell you something more informative than 'object', probably '[object HTMLTableElement]'; also I think that you are trying to ensure that the table variable is a Node? The code you posted would be checking the type of the result of the getElementsByTagName function call - a NodeList.

    Is the objs variable a list of elements? If so, try objs.item(k) instead of objs[k].

  • #6
    New Coder
    Join Date
    Aug 2005
    Location
    Edmonton, Canada
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The source of the problem is the objs Array. In your code you are literally "expecting" there to be table elements in each objs array index. How did you derive the objs Array? Are you certain that each objs array index contains data. Putting a conditional check on the table variable suggests that not all indexes of the objs array contains data because only things like null, 0, false, and undefined should return false inside of the IF statement.


  •  

    Posting Permissions

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