Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery File Upload not working when file input dynamically created

i am using this jquery uploader (http://blueimp.github.io/jQuery-File-Upload/basic.html) and it works fine when the file input is put in the raw code of the site, however i am dynamically appending the fields with jquery and it doesnt work. here is the jquery to trigger the upload:

$('.fileupload').fileupload({
    dataType: 'json',
    done: function (e, data) {
        $.each(data.result.files, function (index, file) {
            alert(file.name);
            //$('<p/>').text(file.name).appendTo(document.body);
        });
    }
});

and this is what SHOULD trigger the upload:

<input class="fileupload" type="file" name="files[]" data-url="uploads/">

Here is the code that is appended by jquery:

$(document).on('click','.addItem', function(){
            $('<!--ROW START-->\
                <form class="widget-content item" data-url="uploads/">\
                    <div class="row">\
                        <div class="col-md-3"><input type="text" class="form-control" name="itemName[]"></div>\
                        <div class="col-md-3"><textarea class="auto form-control" name="itemDescription[]" cols="20" rows="1" style="word-wrap: break-word; resize: vertical;"></textarea></div>\
                        <div class="col-md-3"><textarea class="auto form-control" name="itemCondition[]" cols="20" rows="1" style="word-wrap: break-word; resize: vertical;"></textarea></div>\
                        <input type="hidden" class="itemId" name="itemId[]" value="">\
                        <input type="hidden" name="itemInventoryId[]" value="<?=$_GET["inventory_id"]?>">\
                        <input type="hidden" name="itemParent[]" value="'+$(this).closest('.formHolder').data('parent-room')+'">\
                        <div class="col-md-2">\
                            <div class="fileinput-holder input-group">\
                                <input class="fileupload" type="file" name="files[]">\
                            </div>\
                        </div>\
                        <div class="col-md-1 align-center"><i class="save icon-ok large"> </i>&nbsp;&nbsp;&nbsp;<i class="delete icon-trash large"> </i></div>\
                    </div>\
                </form>\
            <!--/ROW END-->').fadeIn(500).appendTo($(this).parents().siblings('.items'));
            $(this).parent().parent().siblings('.widget-header, .header-margin, .hide').removeClass('hide').fadeIn();
        });

like i say, when i add it into the actual code, not dynamically its fine. Can someone help please?

like image 423
Andy Holmes Avatar asked Oct 19 '13 16:10

Andy Holmes


People also ask

Why is there so many upload fields in my document?

There is also the possibility that user might upload a single document hence, it is not logical to display many upload fields. The solution could be what most sites adopt, to show single file upload at start and provide "upload another file" button.

What happens when user clicks on file upload control?

When user clicks on it, another file upload control gets added in our page. Each text file is in the correct format, with each field starting a new line, and then each record separate with a comma. Download the latest JQuery library.

How to show single file upload at start of the page?

The solution could be what most sites adopt, to show single file upload at start and provide "upload another file" button. When user clicks on it, another file upload control gets added in our page. Each text file is in the correct format, with each field starting a new line, and then each record separate with a comma.

Is there a way to upload a Div without a template?

NOTE: You could theoretically use the .html () method to build the target file upload DIV without the DOM template; however, as I stated above, the more complicated the target element gets, the bigger a pain in the butt this becomes. Hope that helps.


2 Answers

This is because you bind fileupload event before element is added.

Try moving your code into callback function which will be executed after you create input element. Since appendTo() doesn't support callback, you can use each(callback):

$('code_that_you_append').appendTo('some_element').each(function () {
    // here goes $('.fileupload').fileupload({ ... }) function
});

If you need to bind event to .fileupload in multiple places in code, you can create a function to avoid code repetition, like this:

function bindFileUpload() {
    $('.fileupload').fileupload({
        dataType: 'json',
        done: function (e, data) {
            $.each(data.result.files, function (index, file) {
                alert(file.name);
            });
        }
    });
};

and then call it in the callback, like before:

$('code_that_you_append').appendTo('some_element').each(function () {
    bindFileUpload();
});

I've created a little demo. It binds click instead of fileupload to simplify things (fileupload is external plugin...), but the general rule stays the same.

like image 118
Michał Rybak Avatar answered Sep 21 '22 22:09

Michał Rybak


You need to use the jQuery live() function.

This tip I found worked for me.

jQuery fileupload for dynamically added input field

like image 27
Ross Avatar answered Sep 22 '22 22:09

Ross