This is called a closure, unfortunately no other answers contain this very important keyword. I recommend reading this question: How does a javascript closure work ? and this article.
.each()
creates a closure, .prepend()
, unless you're passing a function, doesn't, though it can take a function to solve your current issue, like this:
$('#divStatus div').prepend(function() {
return $(this).attr('id'); //or this.id if you're sure it has one
});
Inside these closures this
refers to the element in the array you're on, outside them, it refers to whatever context you're in, for example:
$(function() { //short for $(document).ready(function() {
$('#divStatus div').prepend($(this).attr('id')) //this refers to document
});
A jQuery object is an array inside, an array of references to DOM elements, whether from a selector finding them, manually added, etc. The functions that loop though these, either .each()
or passing a function to various others, .prepend()
, .attr()
, .css()
, etc all create closures in which this
refers to the element in the array you're currently on when looping through.
I can't stress reading that question and article enough to get a clearer understanding of this, might clear up a few other questions you have as well.