Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getElementsByName works great in netscape, not so for ie?

Status
Not open for further replies.

joelwenzel

Programmer
Jun 28, 2002
448
0
0
<div name=tabBody>
stuff
</div>
<div name=tabBody>
stuff
</div>

<script language=javascript>
//this shows 0 in ie and 2 in netscape
alert(document.getElementsByName(&quot;tabBody&quot;).length)
</script>

I do not know what the code shows 0 in ie and 2 in netscape
 
joelwenzel,

I believe getElementsByName() is a NS proprietary function.

======================================

if (!succeed) try();
-jeff
 
I don't think so. IT's part of the DOM model and microsoft list it on the msdn site.
 
use getElementById()

Not sure what this code is supposed to do... but I think it will cause an error. You have two divs with the same name. Is that legal? I didn't think you could get a .length on a div.

I have used this before:

document.getElementById('divname').getElementsByTagName('div').length;

This will report on all of the <div> tags found within another div.
 
OK...

I stand mistaken. I tried getElementsByName() in IE 5.5 and it worked perfectly when checking:

alert(document.getElementsByName(&quot;tabBody&quot;).length)

I had two divs named just like yours and it alerted 2.

In Netscape 7 pre release... I got 0. I know this is contrary to what you reported above... but these are the results.
 
Worked for me in IE 6. Try tossing quotes around the name in the div tag.

I used your code and your divs and I got back 0 in Mozilla and 0 in IE6. ====================================
I love people. They taste just like
chicken!
 
thanks. That's interesting that it worked for you with your code but not with mine. I will look further into it and see if I can find some other reason it did not work.

I already created a work around (getElementsByTagName(&quot;DIV&quot;) and then comparing the name or each item in the returned collection) but I would rather use the getElementsByName. It seems more efficient.
 
IE5.0 has a faulty getElementsByName function. It exists just like it should, and it's a function just like it should be, and running it doesn't give any errors... But the result is wrong: the returned object has NO elements in it, and the length is 0, although it should be 2. Quite an annoying bug, since it takes a while to figure out why it's not working right.

And I wouldn't suggest ignoring that IE5.0 doesn't support it: Most people use IE5.
Code:
- UNIMENT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top