This is the Jquery code:
$("#save").on ('click', function(){ // save new person
var new_name = $("#name").val();
var new_age = $("#age").val();
var formData = { name : new_name, age : new_age };
$.ajax({
url : "ajax.php",
type: "POST",
data : formData,
success: function(data, textStatus, jqXHR){
get_old_html = $("#result_json").html(); // getting current html data
var array = JSON.parse(data); // getting the JSON
var html = "";
for (var i = 0 ; i < array.length; i++){
var split = array[i].split("||");
html = html + "<tr><td>"+ split[1] +"</td><td>"+ split[2] +"</td><td><center><a href ='#' class ='delete_person' id ='"+ split[0] +"'>X</a></center></td></tr>"; // create the new html with the new item
}
$("#result_json").html(html + get_old_html); // add the new html and the content of it
$("#add_new_person").hide();
$("#add_person").show();
//mesaj de confirmare
},
error: function (jqXHR, textStatus, errorThrown){
console.log(errorThrown);
}
});
});
$(".delete_person").on('click',function(){ //delete person
id = $(this).attr("id");
var formData = { id : id };
$.ajax({
url : "ajax.php",
type: "POST",
data : formData,
success: function(data, textStatus, jqXHR){
var remove = JSON.parse(data); // getting the JSON
if (remove == "success"){
$("#"+id).closest("tr").remove();
}
},
error: function (jqXHR, textStatus, errorThrown){
console.log(errorThrown);
}
});
});
The fact is then when i save a new person and after that I want to delete that person, the onclick event on the class "delete_person" does not work. But when I refreshed the page it works. What should I do ? and all of it is included in $( document ).ready(function() { });