Skip to content

JavaScript confirm delete before delete | Code

Use Window confirm() method in the client-side to confirm delete before delete in JavaScript. When you want to verify the user or delete something, it always a good idea to confirm the request before processing.

var result = confirm("Want to delete?");
if (result) {
    //Logic to delete the item
}

The confirm() method show a dialog box with a message and two buttons (OK and Cancel). This method returns true, if the user clicks OK, otherwise false.

JavaScript confirm delete

Simple example code confirmtion message show on clicking delete (this maybe a button or an image). If the user selects ‘Ok‘ then delete is done, else if ‘Cancel‘ is clicked nothing happens.

This in onclick event of the button:

<!DOCTYPE html>
<html>
<body>
  <button onclick="confirmation()">Delete</button>

  <script>
    function confirmation(){
      var result = confirm("Are you sure to delete?");
      if(result){
        console.log("Deleted")
      }
    }
  </script>

</body>
</html>

Output:

JavaScript confirm delete

You can also use confirm() method to show confirm box on anchor link (a href tag) click. Code snippet show how to display a confirmation dialog when clicking anchor link using onclick() event.

<a href="action.php" onclick="return confirm('Are you sure to delete?')">Delete</a>

Do comment if you have any doubts or suggestions on this Js confirm box topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

1 thought on “JavaScript confirm delete before delete | Code”

Leave a Reply

Your email address will not be published. Required fields are marked *