What is the difference between append() and appendTo() in jQuery? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the difference between append() and appendTo() in jQuery?

Both definition seems to add contents at the end of the selected elements.

4th Jul 2017, 1:17 PM
SC Lee
SC Lee - avatar
2 Answers
+ 3
The difference between append() and appendTo() is in how (in wich order) parameters are passed: // given: var targetElement = $('.myclass'); var newElement = $('<div>content to append</div>'); // both above are equivalent: targetElement.append(newElement); newElement.appendTo(targetElement);
4th Jul 2017, 2:54 PM
visph
visph - avatar
+ 2
Thanks. In terms of adding new elements, they works the same, only the syntax is different. However, there is one difference, appendTo() can move elements around. Check out the code below for example. I have been exploring the code for a bit in code playground. https://code.sololearn.com/Wjsej1Y1VqGr
5th Jul 2017, 7:02 AM
SC Lee
SC Lee - avatar