0

I have a function and it doesn't work in IE11. Display me this error in the forEach. In chrome and firefox work fine. Please help.

Object doesn't support property or method 'forEach'

function someFunction(event) {
  var classList = event.currentTarget.classList.toString();
  var targetClass = classList.toString().slice(classList.indexOf('open'));
  $('button').removeClass('active');

  var toOpen = document.getElementsByClassName(targetClass);
  var _iteratorNormalCompletion = true;
  var _didIteratorError = false;
  var _iteratorError = undefined;

  for (var _iterator = toOpen[Symbol.iterator](), _step
      ; !(_iteratorNormalCompletion = (_step = _iterator.next()).done)
      ; _iteratorNormalCompletion = true) {
    var el = _step.value;

    var childPanels = el.querySelectorAll('.fa');
    childPanels.forEach(function (child) {
      child.classList.toggle('fa-minus');
      child.classList.toggle('fa-active');
    });
    
    var childPanels = el.querySelectorAll('.panel');
    childPanels.forEach(function (child) {
      child.classList.toggle('current');
    });
  }
}
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
Nikita Kurilovic
  • 343
  • 2
  • 3
  • 11
  • 1
    Does this answer your question? [SCRIPT438 Error in Internet Explorer 11](https://stackoverflow.com/questions/41321515/script438-error-in-internet-explorer-11) – Krzysztof Krzeszewski Jul 07 '20 at 11:11
  • Does this answer your question? [JS ForEach Loops in IE11](https://stackoverflow.com/questions/47534102/js-foreach-loops-in-ie11) – Karan Jul 07 '20 at 11:13
  • Yes, I can’t remake my own, I don’t understand how to remake :((( I already saw it, I can’t remake my own, it doesn’t work at all :( – Nikita Kurilovic Jul 07 '20 at 11:13
  • Try like `Array.prototype.slice.call(childPanels).forEach(...)` – Karan Jul 07 '20 at 11:15
  • @Karan please write your answer so that I can mark it. It's work , thanks – Nikita Kurilovic Jul 07 '20 at 11:17
  • You can also add a polyfill `if (window.NodeList && !NodeList.prototype.forEach) {NodeList.prototype.forEach = Array.prototype.forEach;}` – skobaljic Jul 07 '20 at 11:19
  • 1
    Snippet is from answer https://stackoverflow.com/a/54344650/9695286 for which I have already mark question as duplicate. You can delete question or it should be marked as duplicate. – Karan Jul 07 '20 at 11:19

1 Answers1

1

forEach loops aren't supported by IE11, but you can easily get your own forEach :

For example, this :

childPanels.forEach(function (child) {
        child.classList.toggle('fa-minus');
        child.classList.toggle('fa-active');
});

will do the exact same thing than :

for (let idx = 0; idx < childPanels.length; i++) {
        childPanels[idx].classList.toggle('fa-minus');
        childPanels[idx].classList.toggle('fa-active');
}