I have two buttons, an edit and a save. When the user clicks on edit, the input box becomes accessible. How can I get the save button to work properly? Meaning, when the user clicks 'save' the text they have typed in will update to the database. I have the following code, the edit button works but when I click the save button, it disappears and the 'edit' button becomes unaccessible. Please help!
JS:
$(".home").html('<label>Name:</label><input id="editInput" disabled="true" id="userFullName" value="' + ui.fullName + '" type="text"><button class="edit">Edit</button><button class="save">Save</button>');
$(".edit").click(function(e) {
$("#editInput").prop('disabled', false);
});
$(".save").click(function(e) {
$(this).closest('div').find('input').attr('readonly', 'readonly');
$(this).closest('div').find('.save').hide();
$(this).closest('div').find('.edit').show();
var inputValue = $(this).closest('div').find('input').val();
$.ajax({
URL: "https://api.mlab.com/api/1/databases/spk-db/collections/dwUsers/58f62d66c2ef164e6b93a162?apiKey=apiKey",
type: "POST",
data: {
inputValue: inputValue
},
success: function(data) {
swal("Congrats", "Your name has been saved!", "success");
}
});
});
}