I'm using a jQuery to flip an element and would like the feature to remove the cloned element.
When I try to remove the element, for some reason, it does not work because <button>
is between <span class='input-group-btn'>
. When I remove the tag <span>
it works, however, I need this tag for the layout to be as the client desires.
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>
Follow 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?