If you want to separate code snippets to create an element and handle loading events, you can try to have your dynamically created element fire a custom event in the window:
var myElement = $('<img/>', { src: '/images/myimage.png' }).appendTo(document.body); $(window).trigger( {type: "myElementInit", myObject : myElement} );
With the pointer back to itself in the additional parameters, you can have a separate handler setting in jQuery (document) .ready to look for the window event "myElementInit" and grab the link to the element from the additional parameters:
jQuery.('window').bind( "myElementInit", function(event){ var theElement = event.myObject; ... } );
source share