Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnClick Javascript Confirmation Window

Why does the below still delete even if I hit cancel on the alert popup? What am I missing?

onClick="confirm(\'Are you sure you want to delete '.esc_attr($this->event_name).'?\')"
like image 845
RonnieT Avatar asked Feb 12 '12 06:02

RonnieT


People also ask

How can write confirm box in JavaScript?

Syntax. window. confirm("sometext"); The window.

What is window confirm in JavaScript?

window. confirm() instructs the browser to display a dialog with an optional message, and to wait until the user either confirms or cancels the dialog.

How do you make a yes or no question in JavaScript?

You can create a JavaScript confirmation box that offers yes and no options by using the confirm() method. The confirm() method will display a dialog box with a custom message that you can specify as its argument.

What is the difference between confirm and alert method?

Alert box is used if we want the information comes through to the user. Confirm box is used if we want the user to verify or accept something.


1 Answers

If you return false from your onclick handler it will cancel the default action of the click. So try this:

onClick="return confirm(\'Are you sure you want to delete '.esc_attr($this->event_name).'?\')"

That will return whatever value the confirm() returns, i.e., true if you click OK and false otherwise.

like image 138
nnnnnn Avatar answered Sep 26 '22 13:09

nnnnnn