my click on span sends an ajax request and delete clicked todo from database. but in the dom it is not removed until i refresh my page.
my index.html
<ul class="list">
</ul>
in my network tab it sends two xhr requests which i was not mentioned in code.
app.js
$(".list").on("click","span",function(e) {
e.stopPropagation();
deleteTodo($(this).parent());
});
function deleteTodo(el) {
let todoId = el.data("id"), deleteURL = "api/todos/" + todoId;
$.ajax({
method: "DELETE",
url: deleteURL,
}).then(function(data) {
el.remove();
}).catch(function(err) {
console.log(err);
});
}
2
Answers
The above picture shows that the problem was on the server side. In my node backend API's
Router.js
file has a code that redirects it to the/api/todos
url after deleting a todo. this error caused myapp.js
to reject the promise. so i was not able to update the DOM.To remove the element after deleting it from the database, get the el parent element, then make the parent remove it