5

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.

1 Answer 1

9

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`
Sign up to request clarification or add additional context in comments.

2 Comments

This looks like it should work but the serialModel div is never created.
You're totally right. I had a slight typo in mine. I'm off and running now. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.