Search code examples
javascriptfor-loopgoogle-tag-manager

How do i get for-of loop working in google tag manager?


I would like to run the following script:

<script>

    var pixels = document.body.querySelectorAll(':scope > script[id^="tidio"]');
    for (p of pixels) { document.body.removeChild(p); }

</script>

I keep getting the following error:

Error at line 4, character 5: This language feature is only supported for ECMASCRIPT6 mode or better: for-of loop.

Suggestions?


Solution

  • Error at line 4, character 5: This language feature is only supported for ECMASCRIPT6 mode or better: for-of loop.

    this mean that for..of is not supported by your browser, However you still can use forEach method:

    let pixels = document.querySelectorAll("div#root > div")
    pixels.forEach(e => e.parentElement.removeChild(e))
    <div id="root" style="height:20vh;background-color:green">
    <div> 1 </div>
    <div> 2 </div>
    <div> 3 </div>
    <div> 4 </div>
    <div> 5 </div>
    <div>