I have the following piece of Javascript code that works when the DOM is loaded. But when I add new buttons dynamically (after dropzone upload), those new buttons don't respond. What should I add to this script?
document.querySelectorAll('.to-delete').forEach((el) => el.addEventListener('click', function(e) {
e.preventDefault();
if (confirm("Press a button!")) {
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
'id': el.getAttribute('data-delete-attachment'),
})
}).then(response => {
if (response.ok) {
el.closest('.columns').remove();
}
}).catch(err => {
console.error(err);
});
//console.log();
}
}));