[RESOLVED]What is the difference between jquery appendTo and Wrap function

please help me with example to understand the difference and tell me which one we should use when ?

also tell me what is the difference between jquery append() and appendto() ?


According to JQUERY DOCS:

append() Insert content, specified by the parameter, to the end of each element in the set of matched elements.

appendTo() Insert every element in the set of matched elements to the end of the target.

wrap() Wrap an HTML structure around each element in the set of matched elements.

The .append() and .appendTo() methods perform the same task. The major difference is in the syntax-specifically, in the placement of the content and target. With
.append(), the selector expression preceding the method is the container into which the content is inserted. With
.appendTo(), on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted into the target container.

Consider following example as well.

For Example:

 <div id="item">

and for appending some html in it using append() :

$("#item").append("This is appended TExt");

 while in appendTo() we can pass selector.

For Example, if i want to append a p  in the above div i can use
appendTo() this way:

$( "<p>This is appended using append To</p>" ).appendTo( "#item" );

wrap() actually adds a wrapper element for our selected elements.

For Example, in above case if i want another div around item
div i can use wrap() :

$("#item").wrap("<div class='wrapper'></div>");

it will change:

<div id="item">


<div class="wrapper">
<div id="item"></div>

For Reference:




thanks for well explanation.


Please see the answer of Silly Little in the above link.

Leave a Reply