1
The error in the code is that when I click on the "Add" button new input file, the intention is to add only one field after the last one, but the button triples.
My HTML code looks like this:
<div class="col-md-8">
<div class="form-group input-files">
<input type="file" name="imagem_file[]" class="form-control">
</div>
<div class="form-group input-files">
<input type="file" name="imagem_file[]" class="form-control">
</div>
<div class="form-group input-files">
<input type="file" name="imagem_file[]" class="form-control">
</div>
</div>
And the button for action:
<button onClick="addInput();" class="btn btn-sm btn-info">[+]</button>
Javascript looks like this:
<script>
function addInput(){
$(".input-files").after('<div class="form-group input-files"><input type="file" name="imagem_file[]" class="form-control"></div>');
}
</script>
If you leave only one input file field, it generates another, but from there it multiplies by the amount that is being generated.
The example in Codepen.io.
Thank you, that’s right, it worked! in 9min I accept the answer by default here, and to remove I do the same? I use :last with the . remove(); right?
– ElvisP
@Eliseub. Yes, if you want to remove the last
.input-files
that exists– Isac
I had it removed and it worked
– ElvisP