jQuery append child nodes in for each

foreach, jquery

Solution

Within the `each()` function, `this` refers to the thing you're iterating on, in this case the `children()`. It's not the `this` of the original jQuery object.

Therefore:

$(this).children().each(function() {    
    $(div).appendChild($(this));
});

Problem

In the below code I'm trying to loop through each child node and append the child to another element - what is the correct syntax inside the loop? ``` $(this).children().each( $(div).appendChild(this.childNodes.length - 1); ); ```

Original source