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.

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The .append() and .appendTo() methods perform the same task - api.jquery.com/appendTo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.