View Full Version : Problem with getElementsByTagName in IE

01-26-2006, 04:26 PM
I have a problem where:

function myfunction(element, htmlLocation){
var element = document.getElementsByTagName(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?

01-26-2006, 04:56 PM
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


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++){
... do something with myDiv[i]....

01-26-2006, 04:58 PM
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.

01-26-2006, 05:21 PM
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.