Accessing newly added html elements as soon as they are created

Accessing newly added html elements as soon as they are created

Hello,
 
Lets say I have this code:
 
var x = "<div id='div1' />";
$("#main").html(x);
 
then I presume that the following line risks failure because the browser might not have updated the dom just yet
 
$("#div1").html("Hello World");
 
So - is the above line going to work in all browsers ? (like I said I suspect not)
 
And if not, what event fires that I could pick up on to make the above line work ?
 
I tried this
 
$("#main").html(x).ready(function (h) {$("#div1").html("Hello World");};
 
BUT I suspect that ready is going to fire before its ready, because ready only applies to the whole document?
 
Any ideas welcome and if my understanding is completely wrong please say so !! thanks