Opensource, CMS, PHP, MySql, Drupal, Frameworks

Wednesday, February 23, 2011

append() and appendTo() jQuery methods


An easily overlooked difference between append() and appendTo() is in the return value; both return jQuery objects. But where append() returns a jQuery object wrapping the thing(s) that have been appended to, the appendTo() call returns the elements that were appended.


//Create our content    
var appendHTML = "<p>I'm going to be added to the DOM</p>";


//Select our element, then add the html     
$("#container").append(appendHTML);


//Create our content, then select the element to append to
$(appendHTML).appentTo("#container");