tags:

views:

333

answers:

3

I'm using jQuery.append() to add some elements dynamically. Is there any way to get a jQuery collection or array of these newly inserted elements?

So I want to do this:

$("#myDiv").append(newHtml);
var newElementsAppended = // answer to the question I'm asking
newElementsAppended.effects("highlight", {}, 2000);

Thanks

+10  A: 

There's a simpler way to do this:

$(newHtml).appendTo('#myDiv').effects(...);
SLaks
jQuery's appendTo() method can take a selector so: `$(newHtml).appendTo('#myDiv').effects(...);`
UberNeet
I think I actually use `appendTo` and `prependTo` far more than I ever use `append` and `prepend`.
Doug Neiner
@UberNeet: I didn't know that; thanks.
SLaks
neato! thanks...
psychotik
+1  A: 
var newElementsAppended = $(newHtml).appendTo("#myDiv");
newElementsAppended.effects("highlight", {}, 2000);
TiuTalk
Wrong. `append` will return the jQuery object that you called it on, like all other non-getter methods (for the fluent interface).
SLaks
Yeah.. thanks .. Corrected :)
TiuTalk
Thanks for editing. I wanted my `-1` rep back :)
Doug Neiner
+2  A: 
// wrap it in jQuery, now it's a collection
var $elements = $(someHTML);

// append to the DOM
$("#myDiv").append($elements);

// do stuff, using the initial reference
$elements.effects("highlight", {}, 2000);
karim79