Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: append() vs appendTo()

I am testing jQuery's .append() vs .appendTo() methods using following code:

$('div/>', {     id : id,     text : $(this).text()     }).appendTo('div[type|="item"]#'+id); $('div[type|="item"]#'+id).append($(this).text()); 

Note that the selectors are identical in .appendTo() and .append(), yet the latter works (within the same page), while the former does not. Why?

How do I get .appendTo() to work with this type of (complex) selector? Do the two methods interpolate differently? Is there some syntax I'm missing?

I don't want to clutter the post with impertinent code: suffice it to say that elements referenced by selectors exist, as is evidenced by the .append() method producing desired result. Let me know if more info is needed.

Thanks!

like image 402
Yevgeniy Avatar asked Nov 20 '12 17:11

Yevgeniy


People also ask

What does appendTo do in jQuery?

jQuery appendTo() Method The appendTo() method inserts HTML elements at the end of the selected elements. Tip: To insert HTML elements at the beginning of the selected elements, use the prependTo() method.

What is the difference between append and after in jQuery?

. append() adds the parameter element inside the selector element's tag at the very end whereas the . after() adds the parameter element after the element's tag.

What is append and prepend in jQuery?

append() & . prepend() .append() puts data inside an element at the last index; while. . prepend() puts the prepending element at the first index.

How remove and append in jQuery?

jQuery uses: . append(); and . remove(); functions to accomplish this task. We could use these methods to append string or any other html or XML element and also remove string and other html or XML elements from the document.


2 Answers

To answer the question, you don't have an element to appendTo anything, as you're missing characters (in your case it's an opening angle bracket <).

This

$('div/>',{}); 

needs to be

$('<div/>',{}); 

to create an element, otherwise it does exactly what you say it does - nothing!


Otherwise you seem to have the order of things right, it's like this:

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

    $(Append_To_This).append(The_Content_Given_Here); 
  • while .appendTo() works the other way around: it insert every element in the set of matched elements to the end of the target given in the parameter, as in

    $(The_Content_Given_Here).appendTo(Append_To_This); 


There's also .prepend() and prependTo() which works exactly the same, with the only difference being that the prepended elements are added at the beginning of the target elements content instead of the end.

like image 69
adeneo Avatar answered Sep 26 '22 21:09

adeneo


append appends the parameter to the object you're working on.

appendTo appends the object you're working on to the parameter.

More info here: http://api.jquery.com/appendTo/

aside from that, there is something wrong here:

$('div/>', 

this is not selecting anything.

like image 29
René Wolferink Avatar answered Sep 25 '22 21:09

René Wolferink