The code below outputs
#document
#document
My expectation is that it outputs
#document
#head
Why is this not the case?
$(document).ready(function() {
console.log(this);
$("head").each((index, element) => {
console.log(this);
});
});
jQuery's own docs state:
More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element. (https://api.jquery.com/each/)
Possibly I'm missing some subtlety about the .call
method (which is how jQuery invoked the anonymous function in the .each
call) or closures. However, my understanding is that .call
can be used to set the this
parameter.