Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty file input from .clone()

Tags:

jquery

clone

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>
like image 723
MacMac Avatar asked Dec 02 '25 04:12

MacMac


1 Answers

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));
});
like image 106
Naftali Avatar answered Dec 03 '25 19:12

Naftali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!