I am trying to keep my code consistent as I'm using jQuery for my project, however, I'm a bit confused about the each() method of jQuery.
let cells=$(".cell").toArray();
my Vanilla JS code is here:
cells.forEach(function(cell,index){
cell.addEventListener('click',function(){
console.log(cell,index);
})
})
my jQuery is here:
cells.each(function(index,cell){
cell.click(function(){
console.log(cell,index);
})
})
I know the code is wrong, as the console shows Uncaught TypeError: cells.each is not a function but I checked the jQuery Document, it says the each method is the forEach method in JS, I am really confused now.
CodePudding user response:
$(".cell").each is a function. $(".cell").ToArray().each is not a function.
jQuery's each method exists on a jQuery object, not on vanilla JS arrays.
