1
I am using a jQuery to clone an element and would like to have the feature to remove the cloned element.
When I try to remove the element, for some reason, it doesn’t work according to <button>
be among <span class='input-group-btn'>
. How much I remove the tag <span>
works, however, I need this tag for the layout to be as the client wishes.
Follow the HTML code:
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Teste de Adicionar e Remover elementos</h3>
</div>
<div class="panel-body">
<form id="inserir_pessoas" method="POST" action="<?=base_url('cadastro/pessoa_fisica/salvar'); ?>">
<div class="form-group">
<div class="row">
<div class="col-sm-4">
<label for="telefone">Telefone(s)</label>
<div id="telefone" class="input-group">
<span class="input-group-btn">
<select id="sel_tipo_telefone" class="btn btn-default" name="tipo_telefone[]">
<option value="1">Celular</option>
<option value="1">Residencial</option>
<option value="1">Trabalho</option>
<option value="1">Outro</option>
</select>
</span>
<input type="text" class="form-control" placeholder="(11) 5555-5555" name="telefone[]">
<span class="input-group-btn">
<button type="button" class="btn btn-default" onClick="delete_obj(this)">X</button>
</span>
</div>
<span id="novo_telefone"></span>
<p>
<button type="button" onClick="clone_obj('telefone', 'novo_telefone')">Novo Telefone</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
Segue Javascript:
function clone_obj(obj_name, obj_destination) {
$(function(e) {
$('#' + obj_name).clone().appendTo('#' + obj_destination);
});
}
function delete_obj(obj_name) {
$(obj_name).parent('#telefone').remove();
}
Any suggestions?
In your clone function, try changing the id:
$('#' + obj_name).clone().attr('id', 'outro_id').appendTo('#' + obj_destination);
. Ids are and should remain unique.– Oeslei
Hello Oeslei, thanks for the prompt reply. As I am cloning, I would have to put the new id on the delete button tb...
– Evert