0

hello i have some problem with my script: i upload image with jquery ajax:

url:"../ajax/upload_media_files.php",
type: "POST",
data:  fd,
//dataType: 'JSON',
contentType: false,
cache: false,
processData:false,
beforeSend:function(){  
//nothing do
},
success: function (data){   
//if upload success refresh div for preview image               
    update_media_gallery();
}  
function update_media_gallery() {
  $.get("../ajax/reload_media_gallery.php", function(data) {
    $("#list_media_gallery").html(data);
    //window.setTimeout(update, 10000);
  });
}

all works good, now i have on div reloaded button for delete image in this mode:

$('[id^=delete-]').on("click", function(event){
    event.preventDefault(); 
    var elem = document.getElementById(this.id);
    //console.log(elem);
    var db_name = elem.getAttribute("db_name");
    var id_name = elem.getAttribute("row_id");
    var files = elem.getAttribute("del_files");
    if(id_name == ""){  
        Toastify({ text: "Ceva nu a mers bine #id_name, contactati administratorul #enable_id", style: {background: '#F06548'}, duration: 3000 }).showToast();
    } else if (db_name == ""){
        Toastify({ text: "Ceva nu a mers bine #db_name, contactati administratorul #enable_db_name", style: {background: '#F06548'}, duration: 3000 }).showToast();
    } else {  
        $.ajax({  
            url:"../ajax/delete_db.php", 
            method:"POST",  
            data: {id_php: id_name, db_name_php: db_name, del_file: files},
            beforeSend:function(){  
            },              
            success:function(data){  
                confirm("Are you sure?");
            },
            error: function (data) {
                console.log(data);
            }           
        });  
  }  
 });

But now all js script not work, have any ideea how to fixed?

  • Most likely you're overriding the existing elements and the attached events with `$("#list_media_gallery").html(data);`. – Teemu Feb 12 '23 at 15:17
  • this is most likely what is happening, do you have any idea how i can fix this? – Uituceală M.O.D Feb 12 '23 at 15:39
  • [Event delegation](https://stackoverflow.com/q/203198/1169519) does the trick. – Teemu Feb 12 '23 at 15:41
  • Can u offer some example for my code? – Uituceală M.O.D Feb 12 '23 at 17:28
  • There are plenty of examples in the linked post, just apply those to your case. There's not even enough code in your post for making an example. We'd need to know the markup and see how the listeners were originally attached. – Teemu Feb 12 '23 at 17:57

0 Answers0