I have a form, where I make the clone (jQuery) to add new inputs, where I can also remove them. The problem that occurs is: I need to remove the "current" div where the remove button is located. And currently I can only remove the last element.
Form parsed:
Functionsyouaddandremove:
functionaddNovosArquivosUpload(){$('.arquivoUpload').fileinput('destroy');varclone=$('.arquivo_upload:first').clone();varcloneInput=clone.find('input');varindex=$('.arquivo_upload').length;if(cloneInput.val()!==null){cloneInput.val('');}clone.attr('id','arquivoUploadId')$(cloneInput[0]).attr('name','ArquivoDocumental['+index+'][descricao]')$(cloneInput[1]).attr('name','ArquivoDocumental['+index+'][arquivo_url]')$(cloneInput[2]).attr('name','ArquivoDocumental['+index+'][arquivo_url]')$(cloneInput[3]).attr('name','ArquivoDocumental['+index+'][arquivo_url]')$('#row-clone').append(clone);$('.arquivoUpload').fileinput({'language':'pt','showPreview':false,'showUpload':false,'removeLabel':'','browseLabel':''});$('.arquivo_upload:last').append('<divclass="col-md-1"><button type="button" class=" btn btn-sm btn-danger btn-rounded m-t-30 float-right" onclick="removerdivCloneArquivo();"><i class="fa fa-trash"></i></button></div>');
}
function removerdivCloneArquivo(){
$('#row-clone .arquivo_upload:last').remove();
}
Form:
<div class="row">
<button type="button" class="btn btn-info btn-rounded pull-right" onclick="addNovosArquivosUpload();" title="Adicionar mais arquivos">Adicionar</button>
</div>
<div class="row arquivo_upload">
<div class="col-md-6" >
<?= $form->field($model, '[0]descricao')->textInput(['maxlength' => true]) ?>
</div>
<div class="col-md-5">
<?= $form->field($model, '[0]arquivo_url')->widget(\kartik\file\FileInput::classname(), [
'options' => ['multiple' => true, 'class'=>'arquivoUpload'],
'language' => 'pt',
'pluginOptions' => [
'showUpload' => false,
'showPreview' => false,
'browseLabel' => '',
'removeLabel' => '',
],
])->label('Arquivo') ?>
</div>