Search code examples
javascriptconfirm

Javascript confirm return false if cancel


I have a Delete button like so:

<a class="btn btn-default" href="Communities.php?action=delete&id=' . $value['rb_communityId'] . '" onclick="javascript:confirm(\'Are you sure you want to delete this item?\');">Delete</a>

if the user clicks cancel on my confirm back, then I want nothing to happen, is there away if the user clicks cancel it will return false.


Solution

  • <a
      class="btn btn-default"
      href="Communities.php?action=delete&id=' . $value['rb_communityId'] . '"
      onclick="if(!confirm(\'Are you sure you want to delete this item?\')) return false;"
    >
      Delete
    </a>
    

    Key line that changed is the onclick, where the confirmation has been wrapped in an if(!...) to catch the occasions where cancel is clicked, and returns false if that happens.