1

I have link as follows:

<a onclick=delete() href=".$this->action('delete', $data['status_id']).">Delete</a>

the javascript code as follows:

function delete() {
    if (confirm('<?php echo $delConfirmJS ?>')){
         return true;
    }
}

the confirmation message shown, if i cancel message the href link target executes. How to stay it if user cancel confirmation means?

harriyott
  • 10,505
  • 10
  • 64
  • 103
Kumar Shanmugam
  • 597
  • 1
  • 11
  • 40

4 Answers4

4

you have to return the result in onclick (and use quotes!)

<a onclick="return delete()" 
   href=".$this->action('delete', $data['status_id']).">Delete</a>

and remember to return false if the user does not confirm, this is a small change:

function delete() {
    return confirm('<?php echo $delConfirmJS ?>');
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193
2

function delete() {

return confirm("<?php echo $delConfirmJS ?>");

}

If user will press a cancel button confirm will return false. In DOM event handlers return false is the same than prevent default(browser) behavior.

Alex
  • 11,115
  • 12
  • 51
  • 64
2

You need to reverse your logic to prevent the click, rather than allow it:

function delete() {
    if (!confirm('<?php echo $delConfirmJS ?>')){
         return false;
    }
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

Try with:

function delete(e) {
    if (confirm('<?php echo $delConfirmJS ?>')){
         return true;
    }
    e.stopPropagation();
    return false;
}

P.S. and please put your attributes values in quotes, i.e. onclick=delete() should be onclick="delete()".

Krasimir
  • 13,306
  • 3
  • 40
  • 55