Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for of loop querySelectorAll

Mozilla states that "for of loops will loop over NodeList objects correctly". (source: https://developer.mozilla.org/en-US/docs/Web/API/NodeList) However, this doesn't work in Chrome 43. Is this incorrect documentation or a browser bug?

The copied example code used on a page with checkboxes:

var list = document.querySelectorAll( 'input[type=checkbox]' );
for (var item of list) {
  item.checked = true;
}
like image 972
Daniel Herr Avatar asked Jun 15 '15 01:06

Daniel Herr


1 Answers

Edit: This is shipping in Chrome 51.

Jake Archibald posted a simple fix:

NodeList.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]

And for of loops.

like image 96
Daniel Herr Avatar answered Sep 29 '22 03:09

Daniel Herr