Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return confirm cancel button not working

I have this link in twig :

<a href="{{ path('relation-delete', {'id': c.getCustomerId}) }}" 
   onclick="return confirm('{% trans %}relation.delete{% endtrans %}');"
   class="tip" data-original-title="Verwijder klant {{ c.getCustomerName }}">

The HTML in the source :

<a href="/app_dev.php/projects/delete/1" class="tip" 
  data-original-title="Verwijder project Lantaarn plaatsen" 
  onclick="return confirm('Verwijderen');">

<button class="btn btn-danger"><i class="fa fa-times fa-fw"></i></button></a>`

the onlick confirm cancel button doesn't cancel the action but just keeps going. Somebody knows what's wrong with this return confirm ?

like image 898
Tommie Avatar asked Dec 14 '22 20:12

Tommie


2 Answers

You can validade the confirm box outside of html element, in a function and, call this function on 'onclick' event. Like this:

<a href="somePage" onclick="return myFunction()">My link</a>

function myFunction() {
    if (confirm("Confirm message")) {
       // do stuff
    } else {
      return false;
    }
}
like image 193
Helam.Dev Avatar answered Dec 17 '22 09:12

Helam.Dev


Adding event.preventDefault(); fixed it.

Here is an example:

console.log('Validating...');

function confirm_delete(){
var txt;
var r = confirm("Are you sure you want to delete?");
if (r == true) {
    txt = "You pressed OK!";
} else {
    txt = "You pressed Cancel!";
    event.preventDefault();
}
console.log(txt);
}
like image 21
Dhruv Avatar answered Dec 17 '22 10:12

Dhruv