3

I'm trying to clone a file input form, which every time I select a file then click "add more" to clone the file input, but it has copied the selected file in the input.

<input type="file" />
<span id="add-more-files">Add more</span>

jQuery:

$('#add-more-files').click(function()
{
   var cloned = $(this).prev().clone();
   $(cloned).insertBefore($(this));
});

Demo: jsFiddle.

Now whenever you select a file before cloning the previous, you can see it has the same file selected, I need it to be empty when cloned.

EDIT:

How would I be able to clone this, referring to Neal's answer?

<div class="wrap"><input type="file /></div>
<span id="add-more-files">Add more</span>
1
  • what browser are u using? in chrome it gets emptied Commented Apr 8, 2011 at 17:28

3 Answers 3

1

what you can try is:

$('#add-more-files').click(function()
{
   var cloned = $(this).prev().clone();
   cloned.val(null);
   $(cloned).insertBefore($(this));
});

UPDATE
or if all the inputs will be copies from the previous just create a new one instead of cloning:

$('#add-more-files').click(function()
{
   var cloned = $(this).prev();
   $('<input>',{type:cloned.attr('type')}).insertBefore($(this));
});
Sign up to request clarification or add additional context in comments.

8 Comments

This won't work, you can't edit the value of an <input> of type file for security reasons.
His original code doesn't use .val() on an <input> of type file.
@Drackir my new part has nothing to do with val()
You should remove the first part since it doesn't work. The second one should work for what the Asker wants. Also, you don't need to clone the object or wrap this in insertBefore(). Good idea. :)
Pretty good piece of code. Have you tried having a parent wrap (div) and cloning it?
|
1

The only workable solution I found is to provide the file input field as a template that is hidden. Then a clone is created after the page is loaded to provide the first field.

It also works without an "add more" button, instead it adds a new file input field when the last one is filled in.

<div id="files">
    <input type="file" name="files" style="display: none;">
</div>
function AddUploadElement()
{
    var newUploadElement = $("#files").children(":first-child").clone();
    newUploadElement.css("display", "");

    $("#files").append(newUploadElement);
}

$(document).ready(function ()
{
    AddUploadElement();

    $("#files").on("change", "input", function ()
    {
        if ($(this).is(":last-child") == true)
        {
            AddUploadElement();
        }
    });
});

The cloning part is important because the template input field might have additional attributes.

Comments

0

The jquery.fileupload plugin circumvents the issue of clearing file inputs like this:

var inputClone = input.clone(true);
$('<form></form>').append(inputClone)[0].reset();
input.after(inputClone).detach();

i.e. clone the input, append to a temporary form to reset it, and detach the input so that it can be appended to forms consecutively.

then you can input.replaceWith(inputClone);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.