1
  • How could I clear or empty the input type file after I clone the source for it has already a file selected before. I already applied this approach Clearing <input type='file' /> using jQuery seems not working on my set up.

By the way the code is wrap with form element.

== source ==

var source = $( '#table-wrapper' ),
        clone = source.clone( true ),
        count = clone.length;

<div id="table-wrapper">
   <table>
     <tr>
       <td></td>
     </tr>
   </table>
   <p class="pdf-add-row"><input id="pdf" type="file"/></p>
</div>

== clone ==

<div id="table-wrapper-1">
   <table>
     <tr>
       <td></td>
     </tr>
   </table>
   <p class="pdf-add-row"><input id="pdf-1" type="file"/></p>
</div>
Community
  • 1
  • 1
heero
  • 278
  • 4
  • 15

3 Answers3

2

Try this code:

$('button').click(function () {
    var source = $( '#table-wrapper' ),
        clone = source.clone( true ),
        count = clone.length;

    clone.find('input').val('');

    clone.appendTo('#result')
})
Andreas Furster
  • 1,558
  • 1
  • 12
  • 28
1

Do this..

clone = source.clone( true ).find('input').val('')
jay temp
  • 1,207
  • 12
  • 11
0

You can try something like

$('button').click(function() {
  var source = $('#table-wrapper'),
    clone = source.clone(true),
    count = clone.length;

  //append the clone to a temp form so that it can be restted
  $('<form />').append(clone)[0].reset();

  clone.appendTo('#result')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="table-wrapper">
  <table>
    <tr>
      <td></td>
    </tr>
  </table>
  <p class="pdf-add-row">
    <input id="pdf-1" type="file" />
  </p>
</div>
<button>Clone</button>
<div id="result"></div>

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531