Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I store a newly appended element into a variable? [duplicate]

Possible Duplicate:
How can I get a reference to a node directly after it is appended?

as an example:

$(document).append('<div class="new-elem" />');

how do i now store the above newly created element into a variable so i can use it like this:

newElem.append('div class="another-new-element" />');

where newElem is the element i appended to the document initially?

i'm assuming it can't be done like:

var newElem = $(document).append('<div class="new-elem" />');
like image 847
Jai Avatar asked Dec 06 '25 20:12

Jai


2 Answers

You can do the opposite with appendTo:

var newElem = $('<div class="new-elem" />').appendTo(document);
like image 67
VisioN Avatar answered Dec 08 '25 09:12

VisioN


you should use appendTo:

var newElem = $('<div class="new-elem"></div>').appendTo(document);
like image 25
zzzzBov Avatar answered Dec 08 '25 08:12

zzzzBov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!