Search code examples
javascripthtmljqueryonchange

dynamically create input file element in jQuery MultiFile plugin


I'm using jQuery MultiFile plugin, this is a ordinary view of that plugin

enter image description here

this is the HTML syntax for this

<input type="file" id="someName" name="file" class="multi" onchange="return Plugins.handleFileSelect(this);"/>

I'm trying to generate this file input dynamically

So I tried to append this once "click here" button click happens

 <button type="button" id="ifile">click here</button>             
 <div id="target_div"></div> 

<script type="text/javascript">

    $('#ifile').click(function () {
        // when the add file button is clicked append
        // a new <input type="file" name="someName" />
        // to a target_div div
        $('#target_div').append(
            $('<input/>').attr('type', "file").attr('name', "file").attr('id', "someName").attr('class', "multi").attr('onchange', "return Plugins.handleFileSelect(this);")
        );
    });
</script>

but once I do this its generating ordinary file input but its not listing files properly

once I open inspect elements I can see a view like following

enter image description here

how can I generate this properly


Solution

  • You should use MultiFile plugin instead

    $('#ifile').click(function () {
        // when the add file button is clicked append
        // a new <input type="file" name="someName" />
        // to a target_div div
        var input = $('<input/>')
                    .attr('type', "file")
                    .attr('name', "file")
                    .attr('id', "someName");
        //append the created file input
        $('#target_div').append(input);
        //initializing Multifile on the input element
        input.MultiFile();
    });