-1

Question

I have to use a picture, Stackoverflow says:

Oops! Your question couldn't be submitted because: Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.

The screenshot was taken from Stackoverflow's form.

oYes
  • 73
  • 2
  • 9

3 Answers3

8

Internet Explorer has no problems with the length property. If it didn't support it, then it would report undefined not 0.

The HTML is invalid, there is no name attribute for the div element. Internet Explorer is just error recovering in a different way to Firefox and not matching the div elements with getElementsByName

Elements that support both the NAME attribute and the ID attribute are included in the collection returned by the getElementsByName method, but elements with a NAME expando are not included in the collection.

MSDN getElementsByNameMethod

Use a class instead. Internet Explorer 8 doesn't have a native getElementsByClassName, but there are no shortage of cross-browser implementations or you could use a selector engine or a big library that includes one such as YUI or jQuery.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I've used `class` for anything else, and also ID, is there any other way to substitute the `name`? and works in IE of course.. – oYes Jul 19 '11 at 11:12
  • 2
    Eh? That does make much sense. You do know that the class attribute takes a space separated list of classes (plural!)? So if the element already has a class, that won't stop you giving it another one. Either way, `name` is just plain wrong, don't use it. – Quentin Jul 19 '11 at 11:15
3

getElementsByName is for use on input HTML elements - DIV elements do not have name attributes

Manse
  • 37,765
  • 10
  • 83
  • 108
0

Your problem is not that .length is not working on IE, but that getElementByName() is not working on IE... : getElementsByName in IE7

Community
  • 1
  • 1
F-A
  • 643
  • 3
  • 10