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);
);
The .append() method inserts the specified content as the last child of each element in the jQuery collection (To insert it as the first child, use .prepend() ).
jQuery append() MethodThe append() method inserts specified content at the end of the selected elements. Tip: To insert content at the beginning of the selected elements, use the prepend() method.
Syntax of JavaScript appendChild:parentNode. appendChild(childNode); The childNode is the node that we want to append to the parent node parentNode . appendChild() will return the appended child.
The jQuery append() method is used to insert specified content as the last child (at the end of) the selected elements in the jQuery collection. The append () and appendTo () methods are used to perform the same task. The only difference between them is in the syntax.
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));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With