JQuery attaches child nodes for each

In the code below, I'm trying to skip every child of a node and add a child to another element - what is the correct syntax inside the loop?

$(this).children().each(    
    $(div).appendChild(this.childNodes.length - 1);
);
+5
source share
2 answers

Inside the function each() thisrefers to what you are repeating, in this case children(). This is not a thisjQuery source object.

Thus:

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

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.

0
source

All Articles