typeof programmer
typeof programmer

Reputation: 1609

document.getElementsByName.length return 0 in IE

I know document.getElementsByName works differently in IE and Firefox, in IE, it returns HTMLCollection, while in Firefox, it returns NodeList.

In Firefox, the code is working correctly, while in IE, it always return length 0. The IE version is 11.

The code is:

var fullToken = document.getElementsByName("0");
console.log(fullToken.length);

  <span name ="0">h</span>
  <span name ="0">e</span>
  <span name ="0">l</span>
  <span name ="0">l</span>
  <span name ="0">o</span>

Upvotes: 0

Views: 184

Answers (1)

Quentin
Quentin

Reputation: 943579

Your HTML is invalid. The span element cannot have a name attribute.

Internet Explorer appears to be ignoring the name attribute on elements where it is forbidden. If you change a span to an input it will show up in the list.

If you want to describe a group of elements for referencing with JavaScript, use a class with getElementByClassName.

Upvotes: 2

Related Questions