You can use the confirm() method to show a confirmation message before deleting an item using JavaScript.

Here is an example of how you can use it in a delete button’s onclick event:

<button onclick="if(confirm('Are you sure you want to delete this item?')){ deleteItem(); }">Delete</button>

This will display a confirmation dialog box with the specified message and two buttons: “OK” and “Cancel”. If the user clicks “OK”, the deleteItem() function will be executed, otherwise, nothing will happen.

You can replace deleteItem() function with your own function which will be used to delete the item.

You can also use the window.confirm() function, which will work in the same way.

<button onclick="if(window.confirm('Are you sure you want to delete this item?')){ deleteItem(); }">Delete</button>

Also Read:

Categorized in: