There is no Array.prototype.each
method, either use Array.prototype.forEach
or jQuery $.each
utility function. The each
method that you are trying to use, belongs to jQuery objects and is used for iterating through the collections. You could use the $(sections).each(fn)
syntax for creating jQuery object from an array and using it's each
method, but that's a bad idea and should be avoided as the sections
is not an array of DOM elements.
Note that jQuery has 2 each
methods, one is a utility function and another is a method of jQuery object.
You can use this.each
when you are defining a jQuery method as this
in that context refers to a jQuery object which is an array-like object i.e. it has some of the Array methods but it's not a real array, it's a jQuery object, you can call the .get()
method on the jQuery objects for getting the internal arrays, now the returned array has .forEach()
method but not .each()
method.
sections.forEach( function(elem) { console.log(elem); });
$.each(sections, function(index, elem) { console.log(elem); });