RyanPitts
RyanPitts

Reputation: 601

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!

Upvotes: 15

Views: 65878

Answers (7)

djyJK
djyJK

Reputation: 71

In the head tag you can write following code:

<script language="javascript" type="text/javascript">

    function getConfirmation()
    {
        var retVal = confirm("Do you want to continue ?");
        if (retVal == true)
        {
            alert("User wants to continue!");
            return true;
        } 
        else
        {
            alert("User does not want to continue!");
            return false;
        }
    }
</script>

After writing this code you can call this function in the following code:

<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
    CommandName="Edit" Text="Edit" **OnClientClick="getConfirmation()"**>
</asp:LinkButton>

Upvotes: 7

vikas0713
vikas0713

Reputation: 596

May this help someone.

var result = confirm("Are you sure");
return !!result;

Upvotes: 0

Lucas
Lucas

Reputation: 10303

function confirmCancel(){
   var msj='Are you sure that you want to delete this comment?';
   if (!confirm(msj)) { 
      return false;
   } else {
      window.location='backcables.php';
   }
}

Upvotes: 2

Martin
Martin

Reputation: 7

Maybe it's because you set the type=submit in the form that contains the javascript.

You should set it to button or image or whatever if you don't want to be submitted if clicked cancel

<form name="form_name">
    <input type="button" onclick="javascript_prompt()" value="GO"/>
    <input type="hidden" name="new_name" value=""/>
</form>

example of javascript prompt:

function javascript_prompt(){
    var name = prompt("Tell me your name.", "");
    if (name != "" && name != null){
        document.form_name.new_name.value = name;
        document.form_name.submit();
    }else{
        return false;
    }
}

Upvotes: -1

epascarello
epascarello

Reputation: 207511

You are treating the confirm is if it is a if statement, it just returns a Boolean true or false.

if(confirm('foo')){ alert('bar'); }

Upvotes: 3

Amadan
Amadan

Reputation: 198314

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.

Upvotes: 21

Felipe
Felipe

Reputation: 1300

You should return false to prevent the default event. This should work:

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

Upvotes: -1

Related Questions