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
    Jan 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with getElementsByTagName in IE

    I have a problem where:

    Code:
    function myfunction(element, htmlLocation){
    	var element = document.getElementsByTagName(element);
    	alert(element);
    Is returning [object HTMLCollection] in firefox and only [object] in explorer.

    "element" in this case is a <div> tag I have dynamically written a table from a string. The table has already been created by the time this function has been called and the purpose of this function is to apply some code to this div tag to create a "tooltip" hover for the table headers. This works in FireFox 1.5, but not in IE 6 and I assume the problem is in the different values I get for "element".

    I.E. is returning the error

    Error 'document.element' is null or not an object

    any suggestions on the syntax for making this work in both browsers?

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    document.getElementsByTagName() returns a collection of elements/objects. To get one of the elements in that collection, you should treat it like an array. That means you can use the index

    document.getElementsByTagName(tag)[0]

    or you may loop through the collection and find the element upon a condition or criteria:

    var myDivs = document.getElementsByTagName('div')
    for(var i=0;i<myDivs.length;i++){
    if(myDivs[i].className=="mydivclass"){
    ... do something with myDiv[i]....
    }
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    On the other hand you have gave the variable the same name, element same as the passed parameter (which parameter I don't guess what could be)... Change the name of the variable.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you, I will try your first suggestion. I have already tried renaming the variable and that had no effect. To reduce the chances for complication I might leave the variable renamed. I will post after testing.


  •  

    Posting Permissions

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