A IE, you never cease to amaze and surprise me!

A relatively little known, and even less appreciated, way of doing a for loop in javascript works as follows:

for (key in array) {
     var element = array[key];

You might recognize this as essentially the equivalent of the foreach loop in php and other languages. It’s a very useful structure when dealing with associative arrays, or just to save yourself some typing. Unfortunately, there are some quirks with this approach. Notably, as usual, in Internet Explorer.

While at work today I was looping through some form elements, in case the page the script would ran on had multiple forms. Though my test page only had a single form element in it, IE gave me multiple additional elements, such as the letter p, and the number 13…

Worse still, it did the same for the form.elements array within an inner loop. I did some investigation and found that this is a bug in IE, and has been around for a while now.

So thank you IE8, for showing us yet again that as far as you’ve come, you’re still the bottom of the proverbial browser barrel.

Also, thank you Doug for also writing a post on this issue… albeit over 3 years ago 😛 I just felt I should share the fact that this is still very much an issue which IE has, even in its most recent version.

1 Comment

Leave a Reply

Your email address will not be published.

Required fields are marked *

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>