Short version
After calling jQuery.after
to insert HTML, how do I know when the DOM is ready?
Long version
I'm getting Plupload dropzones from an ajax call and using jQuery.after to insert them. When I then call mypluploader.init()
to initialize Plupload, it fails due to the DOM not being ready (apparently). Here's what I'm doing:
- I load the page
- User clicks Edit
- I use ajax to get the Plupload zones and insert the returned HTML with jQuery.after
- I then call
mypluploader.init()
to initialize Plupload - I receive the following error:
TypeError: Cannot read property 'style' of null
Code in summary:
$.get('/plupload-zones.html', function(theDropZoneHTML) {
// theDropZoneHTML: <div id='dropzones'>...</div>
obj.after(theDropZoneHTML);
mypluploader = new plupload.Uploader({..., drop_element: 'dropzones', ...});
mypluploader.init();
//Boom! It fails
});
If I change step 4 to include a setTimeout for 2 seconds it works fine with no error:
$.get('/plupload-zones.html', function(theDropZoneHTML) {
// theDropZoneHTML: <div id='dropzones'>...</div>
obj.after(theDropZoneHTML);
setTimeout(function() {
mypluploader = new plupload.Uploader({..., drop_element: 'dropzones', ...});
mypluploader.init();
//Bingo! It works
}, 2000);
});