willa
willa

Reputation: 669

Javascript confirm Stop page refresh after cancel

I have made a JavaScript function which is attached to the cancel button on of a form. The issue I am finding is that when the cancel button is pressed the page/form reloads losing the data in the text fields.

    function cancelConfirm(){
    var confirmCancel = confirm("Are you sure you wish to cancel?");
    if(confirmCancel==true)
    {
    alert("byebye");
    }
    else
    {
    return false;
    }
    };

I was just wondering how can you prevent the page from reloading after the cancel button on the confirm has been clicked? Thanks for any help you can give

Upvotes: 2

Views: 5183

Answers (3)

Gideon Odiase
Gideon Odiase

Reputation: 1

What you can do is to add an onClick event and pass the event object down to it.

{onClick(event) => {
  event.preventDefault();
  //do something here
}}

A simple event.preventDefault() is what you need.

Upvotes: 0

Sid M
Sid M

Reputation: 4354

function cancelConfirm() {
    var confirmCancel = confirm("Are you sure you wish to cancel?");
    if (confirmCancel == true) {
        alert("byebye");
        return false;// to stop postback on ok cick of confirmation popup
    }
    else {
        return false;
    }
}

Upvotes: 1

john
john

Reputation: 567

you can just use a simple way

<a href="" onclick="return confirm('Are you sure!!!!')">Delete </a>

Upvotes: 1

Related Questions