Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On IE document.getElementsByName won't work

I use this code:

<div name="1234">
   <img src="pic.gif" height="70" width="100" onMouseOver="clear('1234')">
</div> 

And:

function clear(element_name){
    document.getElementsByName(element_name)[0].innerHTML="";
}

It does work in Firefox and Opera, but doesn't work in IE 6.0 or IE 8.0, and probably not even in newer IE's.

What to do?

like image 424
Marcus Avatar asked Jan 29 '13 04:01

Marcus


People also ask

Can I use getElementsByName?

The getElementsByName() accepts a name which is the value of the name attribute of elements and returns a live NodeList of elements. The return collection of elements is live. It means that the return elements are automatically updated when elements with the same name are inserted and/or removed from the document.

What is the difference between getElementsByName and GetElementsByTagName?

The difference is that GetElementByID() will retrieve a single element object based on the unique id specified, whereas GetElementsByTagName() will retrieve an array of all element objects with the specified tag name. Then you can obtain the value using GetElementById from your C++ code. I hope this makes sense.

Does getelementsbyname work in IE6?

It does work in Firefox and Opera, but doesn't work in IE 6.0 or IE 8.0, and probably not even in newer IE's. What to do? Well, the problem is this: IE understands document.getElementsByName (...)

Why can't I get the ID of an element in IE?

Well, the problem is this: IE understands document.getElementsByName (...) [0] as document.getElementById (...). So if you would define also an id for your element, the method document.getElementsByName (element_name) [0].innerHTML="" will surprisingly also work in IE!

Is getElementById case-sensitive in IE8?

The clue is in the method name - getElementBy Id, not getElementBy Name. In IE8 Standards mode, getElementById performs a case-sensitive match on the ID attribute only.

Is there a PowerShell issue with getElementById ()?

This may not be explicitly a Powershell issue - but I noticed something while testing. I can successfully use getElementById () on a Win7 Pro x64 w/ IE11 system using Powershell 3; BUT... getElementById () fails if I use it on a Win7 Pro x64 w/ IE11 system using Powershell *4*.


1 Answers

Well, the problem is this: IE understands document.getElementsByName(...)[0] as document.getElementById(...). So if you would define also an id for your element, the method document.getElementsByName(element_name)[0].innerHTML="" will surprisingly also work in IE!

But since you anyway need to define an id due to IE, and since an id must always start with a char first, you must use:

<div id="a234">
    <img src="pic.gif" height="70" width="100" onMouseOver="clear('a234')">
</div> 

And this command:

function clear(element_id){
    document.getElementById(element_id).innerHTML="";
}

Even more, document.getElementsByName(...)[0] is slower in Firefox: http://www.uize.com/tests/performance/getElementById-vs-getElementsByName.html

So the id definitely wins the race.

UPDATE:

Also important is the fact, that we can adress every id by #a234{...} in a CSS file. So we can define an own style for every id, and this makes the id even more powerful.

like image 154
Marcus Avatar answered Oct 08 '22 20:10

Marcus