I have a table with users and i have a link to delete user from database. Sometimes it works fine, but some times it freezes when i confirm deletion and i have to press "Esc" button for confirm window to disappear. I use "$(document).on('click', function()" because i add users via jquery, and if i use "$(document).ready(function()" newly added users won't delete. Could you please check this script for errors and tell me if it's script issue or something else? May be there is a way to improve it?
Script
$(document).on('click', function() {
$("a:contains('Delete')").click(function(event) {
if(confirm("Are you sure ?")){
$.ajax({
url: $(event.target).attr("href"),
type: "DELETE",
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success: function() {
var tr = $(event.target).closest("tr");
tr.css("background-color","#000000");
tr.fadeIn(1000).fadeOut(200, function(){
tr.remove();})
}
});
} else {
event.preventDefault();
}
event.preventDefault();
});
});
Table cell with delete link
<a href="/delete/${user.login}.json">Delete</a>
UPDATE: I've changed it this way
script
function Delete(event){
if(confirm("Are you sure ?")){
$.ajax({
url: $(event.target).attr("href"),
type: "GET",
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success: function() {
var tr = $(event.target).closest("tr");
tr.css("background-color","#000000");
tr.fadeIn(1000).fadeOut(200, function(){
tr.remove();})
}
});
} else {
event.preventDefault();
}
event.preventDefault();
};
link
<a href="/delete/${user.login}.json" onclick="Delete()">Delete</a>
But now i get stuck on a blank page with the url of my href
value, but user is deleted.