I am new to Javascript and I developed following code which works only once.
function display(arr) {
$(".tbl tbody").empty();
for (i = 0; i < arr.length; i++) {
$(".tbl tbody").append('<tr class="tblrow" id = "'+i+'"> <td><input id="editInput" type="text"/>'+arr[i].Empid+'</td><td>'+arr[i].name+'</td><td>'
+arr[i].pd+'</td><td>'+arr[i].unplannedleaves+'</td><td>'+arr[i].response_time+'</td><td class = "tt"><button class="delbtn" id ="btn'+i+'">Delete</button></td></tr>');
$("td").css("text-align", "center");
$("td").css("border", "1px solid black");
$(".tt").css("border", "none");
$(".tbl").css("border", "none");
$(".delbtn").css("display", "none");
}
}
$("tr").on('dblclick', function() {
var s = this.id;
$("#btn" + s).css("display", "block");
});
$(".delbtn").click(function() {
var s = this.id;
var k = s.slice(3);
arr.splice(k, 1);
display(arr);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="addelem">
<table class="tbl1">
<tr>
<td>
<input id="empid" type="text" placeholder="EmpId">
</td>
<td>
<input id="name" type="text" placeholder="Name">
</td>
<td>
<input id="pd" type="number" placeholder="Pds Delivered">
</td>
<td>
<input id="unp" type="number" placeholder="Unplanned leaves">
</td>
<td>
<input id="rsp" type="number" placeholder="Response Time">
</td>
</tr>
</table>
<button id="submit">Submit</button>
<button id="cancel">Cancel</button>
</div>
After my table gets displayed usingdisplay
method. The first time I dblclick a row it works as expected and displays the delete button. I click delete the row gets deleted and the table is re-rendered. However, after this the row is no more dbl-clickable. I tried debugging but the control won't even go in the dblclick listener.
Can someone explain why is it so. Whats going wrong in here?
Bind your tr element like this:
$(document).on('dblclick', "tr", function() {
var s = this.id;
$("#btn" + s).css("display", "block");
});
It will dynamically subscribe any new tr with your dblclick event