weird javascript failure in IE with attribute name=item

  • Thread starter Thread starter btknorr
  • Start date Start date
B

btknorr

The following html and javascript combination fails to execute in
Internet Explorer...does anyone know why? If you change the input's
attribute "name" to anything other than "item" it works just fine. Any
help is greatly appreciated...thanks.

<html>

<input type="button" name="item" value="My name is item">

<script>
alert("hello");
alert(document.getElementsByTagName("input").item(0));
alert("hello");
</script>

</html>
 
Hi BT,
I 'm no expert in this but I think you should call it hallo because it the
variables for the event handler to be called.
Hope it helps
nass
 
(e-mail address removed) schreef:
The following html and javascript combination fails to execute in
Internet Explorer...does anyone know why? If you change the input's
attribute "name" to anything other than "item" it works just fine. Any
help is greatly appreciated...thanks.

<html>

<input type="button" name="item" value="My name is item">

<script>
alert("hello");
alert(document.getElementsByTagName("input").item(0));
alert("hello");
</script>

</html>

If you write document.getElementsByTagName("input"), you create a
collection with all input elements. If you had named it, for example,
myInput, you could do:
document.getElementsByTagName("input").myInput
to get a reference to it. As you have named it item, you can call it using:
document.getElementsByTagName("input").item
Note that item is the name of the element. But by naming it item, you
overwrite the standard function item() that all collections have.
So you can't do item(0) any more, because the item function has been
overwritten by a reference to that html element.
 
Thanks for the explanation. It's interesting to note that if you have
multiple tags on the page, if just one of them has either name=itme or
id=item and then you can no longer access any of the elements by
item(index). For example the following fails:

<html>

<input type="button" name="wow" value="wow">
<input type="button" name="wow2" value="wow2">
<input type="button" name="wow3" value="wow3">
<input type="button" name="wow4" value="wow4">
<input type="button" name="wow5" value="wow5">

<input type="button" name="item" value="My name is item">

<script>
alert("hello");
//try to access wow2 by index - this fails
alert(document.getElementsByTagName("input").item(1));
alert("hello");
</script>
</html>
 
(e-mail address removed) schreef:
Thanks for the explanation. It's interesting to note that if you have
multiple tags on the page, if just one of them has either name=itme or
id=item and then you can no longer access any of the elements by
item(index).

Of course it fails, the item() function is overwritten by a reference to
the element named item, so the function doesn't work for any element. I
think you could still do this:
var inputs = document.getElementsByTagName("input");
alert(inputs[1]);
just not:
alert(inputs.item(1));
 
Back
Top