In JavaScript we can display a confirmation modal or pop on click of delete button. The confirmation modal asks for user confirmation for deleting. Below is the mentioned code for confirmation message for a user before deleting.
<!DOCTYPE html> <html> <body> <button onclick="confirmation()">Delete</button> <script> function confirmation(){ var result = confirm("Are you sure you want to delete?"); if(result){ console.log("Deleted") } } </script> </body> </html>
In The above html code we have created a delete button which when clicked calls the confirmation() function.
The confirmation() function is declared in the scripts tag. Confirm method takes a string as its parameter. Here we have mentioned “Are you sure you want to delete ?” message. confirm() is a javascript method displays a dialog box with a message, with an OK button and a Cancel button.
This method returns true if the user clicked “OK” else it returns false.
In the Confirmation function, we have created an if condition wherein the condition returns true it display’s “Delete” message in the console.
Following are the images for the output of for the confirmation message modal code.
Here in the above image we can see the “Delete” message in the console after clicking on the OK button.