RonnieT
RonnieT

Reputation: 2203

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).'?\')"

Upvotes: 7

Views: 43236

Answers (5)

Afscell  Muhammed
Afscell Muhammed

Reputation: 139

You need to return the value from that confirm:

onClick="return confirm('Are you sure you want to delete?')"



Upvotes: 1

Fahim Parkar
Fahim Parkar

Reputation: 31647

Check below..

<html>
<script language="javascript">
function checkMe() {
    if (confirm("Are you sure")) {
        alert("Clicked Ok");
        return true;
    } else {
        alert("Clicked Cancel");
        return false;
    }
}
</script>
<body>
<form name="myForm">
<input type=submit value="Press Me" onClick="return checkMe()">
</form>
</body>
</html>

Write what you want to do in Click Ok.

Good Luck!!!

Upvotes: 4

gdoron
gdoron

Reputation: 150263

You need to return the value from that confirm:

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

So if you click cancel it will be equal to onClick="return false"

Upvotes: 3

nnnnnn
nnnnnn

Reputation: 150040

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.

Upvotes: 22

summea
summea

Reputation: 7593

You might need to make sure to use return false; when the user clicks on the cancel button, in your Javascript code. Otherwise, the script won't cancel the rest of the process.

Upvotes: 0

Related Questions