jmease
jmease

Reputation: 2535

JQuery Append To Dynamically Created Element

I am dynamically appending a div element to an existing div. But immediately following that, I need to append another div to the div I just created dynamically. But I can't seem to find the dynamically created div in order to append to it. I assume maybe the DOM isn't aware of that div yet since I just made it. How do I do this?

var serialModel = "Test Test";
$("#existingDiv").append("<div id = '" + serialModel + "'></div>");
$("#" + serialModel).append("<div>content here</div>")

The last line doesn't do anything. The second line produces the new div, but then I can't find it to append to it.

Upvotes: 5

Views: 12972

Answers (1)

VisioN
VisioN

Reputation: 145398

What if you do it vice versa:

$("<div />", { id : serialModel })     // create `<div>` with `serialModel` as ID
    .append("<div>content here</div>") // append new `<div>` to it
    .appendTo("#existingDiv");         // append all to `#existingDiv`

Upvotes: 9

Related Questions