How to add delete button with confirm window



First we use the code of button and anchor tag as een below 





echo '<td>'.'<button>'.' <a href="#" class="delete" data-confirm="Are you sure to delete this post?">Delete</a>'.'</button>'.'</td>';

href we link the page of delete.php

now we use the javascript 



<script>

var deleteLinks = document.querySelectorAll('.delete');

for (var i = 0; i < deleteLinks.length; i++) {
deleteLinks[i].addEventListener('click', function(event) {
     event.preventDefault();

     var choice = confirm(this.getAttribute('data-confirm'));

     if (choice) {
     window.location.href = this.getAttribute('href');
     }
});
}
</script>




Comments