I want my table to actually "save" the input (the input dialog box should disappear and the new value should take place of the old value - of course until the next reload). Now it stays in the dialog box after the click out.
How to fix it?
function edit_description() {
var targetDescription = $("#description-1");
var value = targetDescription.text();
if (value != "") {
value = ""
};
targetDescription.html(`<input class="description form-control" data-target="description-1" type="text" value=${value}>`);
$("input:text").focus();
$("input").blur(function(e) {
e.preventDefault();
var target = targetDescription.attr("data-target");
$(`#${target}`).text($(this).val());
var description = $(this).val();
save_description(identification = "description-1", description);
});
};
function save_description(identification, description) {
console.log('Saved!');
var userInput = {
"identification": identification,
"description": description
};
};
a[role="button"][onclick] {
color: blue;
cursor: pointer;
text-decoration: underline;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="table-wrapper">
<table id="table" class="table table-striped">
<thead>
<tr>
<th scope="col"><span>Edit</span></th>
<th scope="col"><span>Description</span></th>
</tr>
</thead>
<tbody>
<tr>
<td id="edit-1"><a class="btn" role="button" onclick="edit_description();">Edit ></a></td>
<td id="description-1">Lorem</td>
</tr>
</tbody>
</table>
</div>
.html( )with an interpolated string without even attribute-delimiting quotes means you won't generate valid output HTML.