Search code examples
javascriptjquerydomjquery-file-uploaddynamically-generated

fileupload Jquery doesn't work dynamically


I use this file-upload script: http://tutorialzine.com/2013/05/mini-ajax-file-upload-form/

There is this in it:

$('#upload').fileupload({

My Problem is, that i load the content dynamically, so I think I need something like this (because I know this problem from the .click() function):

$(document).on('fileupload', '#upload', function () {

But this doesn't work. Can anyone help me, how to get this function called when the content with the form with id="upload" is loaded dynamically?

Would be great!


Solution

  • Your issue is probably because you're initializing $('#upload').fileupload() in document.ready().

    You should initialize the element as file upload (execute $('#upload').fileupload()) after you dynamically load the content.

    Since you're loading the content via ajax on button click (according to comments), you code should be along the following:

    $(":button").click(function(){
     $.ajax("url",{
       success: function (data){
         //code for injecting the element into DOM
         $('#upload').fileupload(); // initialize it as file upload
       }
     });
    });