Are HTMLCollection and NodeList iterables?
Symbol.iterator support for NodeList, HTMLCollection, DOMTokenList, and DOMSettableTokenList was discussed and added to the WHATWG’s DOM spec last year.
Symbol.iterator support for NodeList, HTMLCollection, DOMTokenList, and DOMSettableTokenList was discussed and added to the WHATWG’s DOM spec last year.
DOM4 now defines NodeList as an iterable: iterable<Node>; According to the IDL draft, that means An interface can be declared to be iterable by using an iterable declaration (matching Iterable) in the body of the interface. iterable<value-type>; iterable<key-type, value-type>; Objects implementing an interface that is declared to be iterable support being iterated over to obtain … Read more
Seems like you can use the same Array.prototype.slice.call that makes the args array-like object become an array. (See here) var inputs = document.getElementsByTagName(‘input’); var selects = document.getElementsByTagName(‘select’); inputs = Array.prototype.slice.call(inputs); selects = Array.prototype.slice.call(selects); var res = inputs.concat(selects); alert(res.length);
There is no way to do it without looping through every element. You could, of course, write a function to do it for you. function addEventListenerList(list, event, fn) { for (var i = 0, len = list.length; i < len; i++) { list[i].addEventListener(event, fn, false); } } var ar_coins = document.getElementsByClassName(‘coins’); addEventListenerList(ar_coins, ‘dragstart’, handleDragStart); or … Read more
Information about each method details if it is live or not, but there does not seem to be a standard convention for determining it. document.getElementsByClassName() is an HTMLCollection, and is live. document.getElementsByTagName() is an HTMLCollection, and is live. document.getElementsByName() is a NodeList and is live. document.querySelectorAll() is a NodeList and is not live. HTMLCollections appear … Read more
[] is an array. This array isn’t used at all. It’s being put on the page, because using an array gives you access to array prototypes, like .forEach. This is just faster than typing Array.prototype.forEach.call(…); Next, forEach is a function which takes a function as an input… [1,2,3].forEach(function (num) { console.log(num); }); …and for each … Read more
With ES6, we now have a simple way to create an Array from a NodeList: the Array.from() function. // nl is a NodeList let myArray = Array.from(nl)