Question

Hi all I am working in angular js , I want to add confirm message when click delete button

I tried this code :

<a data-ng-click="removeTodo(this)" href="javascript:;"
onclick="javascript:return confirm('Are you sure you want to delete ?')">Delete</a>

This is shown ok and cancel button with the confirm box , if i click the ok button it's going to next process , but if i click the cancel button then it's same as going to same process confirm box was not hide ( The ok button will return true , but same as cancel button will be return true not a false)

But it's working good when i used this below code on click my delete button

 $scope.removeTodo = function (Student) {
        if (!confirm("Do you want to delete this ? ")) {
            return false;
        }

   //please think , I added Some code for next process in here 
};

What is the problem ? what i missed ? Why my first code was not working ?

Was it helpful?

Solution

It is because return false is same as preventing the default action and stopping event propagation - which will prevent the handlers attached to ancestor elements from being executed... but in your case both click handlers are attached to the same element.

So even if the confirm returns false the angular click handler will get executed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top