ie7 cant loop over string with javascript

552 Views Asked by At

Anyone knows why this isnt working in IE7 and how I can make it work?

var test = "hello";
for (var i = 0, len = test.length; i < len; i++) {
    alert(test[i]);
}

or see http://jsfiddle.net/75Cqt/

2

There are 2 best solutions below

0
T.J. Crowder On BEST ANSWER

IE7 doesn't support indexing into strings with [i], you have to use charAt(i). E.g.:

var test = "hello";
for (var i = 0, len = test.length; i < len; i++) {
    alert(test.charAt(i));
}
0
HBP On

Try test.charAt(i) in place of char[i]