In the following snippet I have a input and a delete button next to it. They can be cloned and each delete button has a function that tells me which input is trying to be deleted.
The weird thing is that it works perfectly when I run the snippet.
In my browser the cloned buttons are not calling the function, only the original one does. Also, the title "Remove" appears only on the first button, even when I hover the mouse on any other button.
What could be causing this issue?
I am using the same versions of everything in the snippet.
$("#clone").click(function() {
var currentCount = $("#tablePhones tbody tr").length;
var newCount = currentCount + 1;
$('#tablePhones tbody>tr:last').clone(true).insertAfter('#tablePhones tbody>tr:last');
$('#tablePhones tbody>tr:last').find("input, a").each(function() {
var newId = $(this).attr("id").replace("_" + currentCount, "_" + newCount);
var newName = $(this).attr("name").replace("_" + currentCount, "_" + newCount);
$(this).attr("id", newId).attr("name", newName);
if ($(this).is("input")) {
$(this).val("");
}
});
});
$(".btn.remove").click(function() {
name = $(this).attr("name");
console.log(name);
});
td .btn.aligned {
position: absolute;
margin-top: 7px;
float: right;
margin-left: 5px;
}
td input {
float: left;
margin-bottom: 10px;
}
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://use.fontawesome.com/a06b1c7829.js"></script>
<a id="clone" class="btn btn-primary">
<i class="fa "></i> Clone
</a>
<table id="tablePhones" class="table table-hover">
<thead class="thead-inverse">
<th>Phone numbers</th>
</thead>
<tbody>
<tr>
<td>
<div class="row col-xs-3">
<input type="text" name="phone_1" id="telefono1_1" class="form-control" />
<a title="Remove" name="removephone_1" id="removephone_1" class="btn btn-danger btn-xs aligned remove"><span class="fa fa-times">
</span>
</a>
</div>
</td>
</tr>
</tbody>
</table>