How to show a confirm message before delete using javascript

Generally When you perform the CRUD operations  on any application . To warn a user when user trying to remove / delete anything from the displayed data from the database.

I.e " Are you sure you want to delete this item ? ".

When user click on the remove link it will pop up the confirm message . Here user press on the ok key then that item will be removed otherwise nothing will happen.


This can be done in two different ways
  1. You can create the JavaScript function and call this function from the anchor href tag
  2. You can directly call the confirm function from the on click event.
First Way
JavaScript Function
 <script>  
 function confirmDelete(delUrl) {  
  if (confirm("Are you sure you want to delete this item")) {  
      document.location = delUrl;  
  }  
 }  
 </script> 
Function calling
 <a href="javascript:confirmDelete('delete.php?id=1')">Delete</a>  
Second way 
<a href="delete.php?id=1" onclick="return confirm('Are you sure you want to delete this item ?')">Delete</a>  
Output
Delete

* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff


EmoticonEmoticon