Reputation:
I have this code in javascript:
var prop="author";
var optele = theform.getElementsByTagName("input");
alert(optele[prop]);
but in Chrome it returns "undefined" even though I'm sure that the "author" tag exists. Instead, work well in IE and Firefox.
But I noticed that there is a "namedItem" method in optele var in IE and Firefox but there isn't in Chrome.
Someone can help me to run similar code syntax on Chrome?
Upvotes: 1
Views: 2049
Reputation: 816780
That's because IE and FF return a HTMLCollection
[MDN] instead of a NodeList
[MDN]. NodeList
's don't have a way of extracting an element by name or ID (what the namedItem
method [MDN] does).
The solution is to not use this feature, but instead iterate over the nodes and compare the name
property. Alternatively you could use document.getElementsByName
[MDN].
From the MDN getElementsByTagName
documentation:
Note: While the W3C specification says
elements
is aNodeList
, this method returns aHTMLCollection
both in Gecko and Internet Explorer. Opera returns aNodeList
, but with anamedItem
method implemented, which makes it similar to aHTMLCollection
. As of January 2012, only in WebKit browsers is the returned value a pureNodeList
. See bug 14869 for details.
Upvotes: 6