You must use the callback function or anonymous function in the call each:
$(this).children().each(function() {
$(div).appendChild(this.childNodes.length - 1);
});
or
function doSomething() {
$(div).appendChild(this.childNodes.length - 1);
}
$(this).children().each(doSomething);
I'm not sure that your code has not been improved, but I can tell when I see only a small part of it.
source
share