2

I'm looking for jQuery's implementation of the.appendTo function.
I tried looking for the function in: http://code.jquery.com/jquery-latest.js, but I could find only 2 mentions of it, and both don't include a definition.

Any ideas where I can find it?

Thank you, Niv.

Niv
  • 2,294
  • 5
  • 29
  • 41

1 Answers1

1

It's actually quite cool: appendTo is dynamically defined relative to append. That's what is going on in these lines:

jQuery.each({
    appendTo: "append",
    prependTo: "prepend",
    insertBefore: "before",
    insertAfter: "after",
    replaceAll: "replaceWith"
}, function( name, original ) {
    jQuery.fn[ name ] = function( selector ) {
        var ret = [],
            insert = jQuery( selector ),
            parent = this.length === 1 && this[0].parentNode;
...

Which makes sense, as the two functions are almost the same in functionality, just reversed in which effects which.

Should you include that file, then pass the function $('html').appendTo and analyze what it references, you get this:

function ( selector ) {
    var ret = [],
        insert = jQuery( selector ),
        parent = this.length === 1 && this[0].parentNode;
...

Do you recognize that code? If you continue down the first code block I mentioned, you'll see they're almost identical. The first block dynamically generates the code for appendTo, as well as a few others in the same vein.

Nick
  • 8,964
  • 4
  • 26
  • 37
  • `The .append() and .appendTo() methods perform the same task` - http://api.jquery.com/appendTo/ – laker Aug 09 '12 at 15:59