Skip to content Skip to sidebar Skip to footer

Doesn't The Append Function Return The Appended Object?

Assuming this is some existing block level element in the dom and image is http://www.google.com/images/srpr/nav_logo25.png doing the following does not work: $(this).append('

Solution 1:

.append() returns the object you appended to, to get the object you appended in the chain, use .appendTo() instead:

$('<img>').appendTo(this).load(function() { alert('loaded'); })
                         .attr('src', opts.image);

Both versions return what was in the chain previously...but since you want to deal with the <img> use the .appendTo() form. Also, for reliable results with your load, attach your load handler before setting the src as I have above.

Solution 2:

No, append returns the jQuery object to which you are appending. From the source (1.4.4)

append: function() {
    returnthis.domManip(arguments, true, function( elem ) {
        if ( this.nodeType === 1 ) {
            this.appendChild( elem );
        }
    });
}

Post a Comment for "Doesn't The Append Function Return The Appended Object?"