0

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:

  1. I load the page
  2. User clicks Edit
  3. I use ajax to get the Plupload zones and insert the returned HTML with jQuery.after
  4. I then call mypluploader.init() to initialize Plupload
  5. 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);
});
Daniel Flippance
  • 7,734
  • 5
  • 42
  • 55

1 Answers1

0

Aha, the delay was from an Angular repeater which is also on the page. Waiting for the Angular repeat to finish solved the problem: ng-repeat finish event

Community
  • 1
  • 1
Daniel Flippance
  • 7,734
  • 5
  • 42
  • 55