Milo-J Milo-J - 16 days ago 11
HTML Question

Jquery - .forEach() not working in IE8

I have created this little interaction for one of the platforms at work - http://jsfiddle.net/S79qp/426/

It works fine in all browsers apart form IE8. When I run the console it seems to be this section that it is having problems with...

Array.prototype.forEach.call(l, function(item) {
a.push(jQuery(item).text());
});


Can someone show me an IE8 friendly alternative so I can make it compatible for the versions required?

Answer

Use the jQuery.each method:

jQuery.each(l, function(index, item){
  a.push(jQuery(item).text());
});

If the target array is empty from start, you can use the jQuery.map method for this instead:

var a = jQuery.map(l, function(item){
  return jQuery(item).text();
});