Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript confirm cancel button not stopping JavaScript

I have a delete button that is tied to some comments on a page i have. When you click the delete button i am trying to get a confirm dialog box to pop up asking if you are sure you want to delete the comment. Clicking OK should run the function to delete the comment and clicking cancel should not run the function but simply close the dialog box.

This is my code:

onclick="confirm('Are you sure that you want to delete this comment?'); commentDelete(1);"

My problem: When i click cancel the delete function still runs. My guess is that the function is still getting called because when i click cancel it just is stepping forward in the JavaScript and calling the function. How can i accomplish this correctly? I know this is probably a simple problem. Thanks for any help!

like image 472
RyanPitts Avatar asked Apr 29 '11 16:04

RyanPitts


People also ask

How does confirm work in Javascript?

The confirm() method is used to display a modal dialog with an optional message and two buttons, OK and Cancel. It returns true if the user clicks “OK”, and false otherwise. It prevents the user from accessing other parts of the page until the box is closed. message is the optional string to be displayed in the dialog.

What value is returned when a confirm box is Cancelled in Javascript?

Confirm Box If the user clicks "Cancel", the box returns false.

How do you make a confirmation pop up in HTML?

The confirm() method displays a dialog box with a message, an OK button, and a Cancel button. The confirm() method returns true if the user clicked "OK", otherwise false .


Video Answer


1 Answers

onclick="if (confirm('Are you...?')) commentDelete(1); return false"

You are missing an if. In your version, first you get a question, and then regardless of the answer, you call commentDelete.

like image 77
Amadan Avatar answered Sep 18 '22 07:09

Amadan