I'm trying to edit the values of bootstrap table on button click using Jquery. I've taken reference from this post making row editable and it works exactly fine.The only problem that I am having is to double click the button to enter the edit mode.Here is my code snippet
$('.editbtn').click(function () {
var currentTD = $(this).parents('tr').find('td');
if ($(this).html() == 'Edit') {
currentTD = $(this).parents('tr').find($("td").not(":nth-child(1)"));
$.each(currentTD, function () {
$(this).prop('contenteditable', true).css({
'background':'#fff',
'color':'#000'
})
});
} else {
$.each(currentTD, function () {
$(this).prop('contenteditable', false).removeAttr("style");
});
}
$(this).html($(this).html() == 'Edit' ? 'Save' : 'Edit')
if ($(this).html() == 'Save'){
$(this).prop('contenteditable',false)
}
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<table id="tableone" class="table table-striped table-dark table-bordered" border="1">
<thead>
<tr>
<th scope="col">Date</th>
<th scope="col">Name</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>21st August</td>
<td>abc</td>
<td contenteditable="false"><button type="button"class="btn btn-primary editbtn">Edit</button>
<button type="button" class="btn btn-danger">
Delete
</button></td>
</tr>
<tr>
<td>21st August</td>
<td>abc</td>
<td contenteditable="false"><button type="button" class="btn btn-primary editbtn" >
Edit
</button>
<button type="button" class="btn btn-danger">
Delete
</button></td>
</tr>
<tr>
<td>21st August</td>
<td>abc</td>
<td contenteditable="false"><button type="button" class="btn btn-primary editbtn" >
Edit
</button>
<button type="button" class="btn btn-danger">
Delete
</button></td>
</tr> <tr>
<td>21st August</td>
<td>abc</td>
<td contenteditable="false"><button type="button" class="btn btn-primary editbtn">
Edit
</button>
<button type="button" class="btn btn-danger">
Delete
</button></td>
</tr>
</tbody>
</table>
Here I've disabled editing the data from one column.As you can see,when we click the edit button in first row,only a single click is required but for remaining rows,multiple click is necessary.And after multiple clicks,it would be in edit mode on single click until refreshed the page.I don't know where I missed.I would be grateful for your help.