I\'m having a hard time converting a NodeList
to an array in IE 8. The following works perfectly in Chrome, but in IE 8 toArray()
is not recognize
Old question, but here is a tried & true method:
var nodes=document.querySelectorAll("div"); // returns a node list
nodes=Array.prototype.slice.call(nodes); // copies to an array
Explanation
document.querySelectorAll
uses a CSS-style selector to find elements and returns a node list. It works as of IE 8.slice
method copies a portion of an array-like collection (in this case all of it) into a new array.call
allows you to borrow a method from one object to use on anotherTo find the node list you could also have used `document.getElementsByTagName(), but this one is more flexible.