Table first row text editable is not working. I have two button one is edit and another one is cancel. If I click edit button I want to edit first td text with textbox and again if I click save button I want to save that data. If I click cancel button I want to show previous value. I tried but not working. please help.
html:
<table id="tabledata">
<tr>
<th>RecID</th>
<th>Col1</th>
<th>opt</th>
</tr>
<tr>
<td><a><div class="nestedtable"> </div></a>RecID</td>
<td>Val1.1</td>
<td><button class="edit">edit</button><button class="">cancel</button></td>
</tr>
<tr>
<td><a><div class="nestedtable"> </div></a>RecID</td>
<td>Val2.1</td>
<td><button class="edit">edit</button><button class="cancel">cancel</button></td>
</tr>
<tr>
<td><a><div class="nestedtable"> </div></a>RecID</td>
<td>Val3.1</td>
<td><button class="edit">edit</button><button class="cancel">cancel</button></td>
</tr>
javascript:
$(function () {
$(".edit").click(function (e) {
e.preventDefault(); // <-- consume event
e.stopImmediatePropagation();
$this = $("#tabledata:first td");
if ($this.data('editing')) return;
var val = $this.text();
$this.empty()
$this.data('editing', true);
$('<input type="text" class="editfield">').val(val).appendTo($this);
$this.text("save");
$this.addclass('savefield')
});
putOldValueBack = function () {
$("#tabledata .editfield").each(function(){
$this = $("#tabledata:first td");
var val = $this.val();
var td = $this.closest('td');
td.empty().html(val).data('editing', false);
$this.text("edit");
$this.addclass('editfield')
});
}
$(document).click(function (e) {
$(".savefield").click(function (e) {
putOldValueBack();
});
$(".cancel").click(function (e) {
//cancel editable and show previous value show
});
});
});