2

I need this function to fail and not do the confirm, or submit the form and stay on the page if the sub function comes back false.

   function doDelete()
{
   if (THIS FAILS!()-STAY ON PAGE!) 
       // do something here
if (confirm("Are you sure you want to update and then delete this hot part?"))
    {
        return true;
    }
    else
    {
        return false;
    }
}
1
  • 2
    how is this function getting called? Commented Dec 30, 2011 at 14:39

4 Answers 4

2
function doDelete()
{
    if (THIS FAILS!()-STAY ON PAGE!) {
        return false;
    } else {
        return confirm("Are you sure you want to update and then delete this hot part?");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Returning false from a form submit callback will result in the page not being submitted. You can also just return the result of the confirm, rather than using an if-else.

function doDelete() {
    if (THIS FAILS!()-STAY ON PAGE!) {
        // do something here
        return false;
    }
    return confirm("Are you sure you want to update and then delete this hot part?");
}

Comments

0

You can attach a function to a form that will get called when the user attempts to submit the form. If that function returns false, the form won't be submitted and the user will remain on the page.

Essentially, you can do this:

<script type="text/javascript">
    function doDelete()
    {
        if (THIS FAILS!()-STAY ON PAGE!)
            return false;
        return confirm("Are you sure you want to update and then delete this hot part?");
    }
</script>

<!-- Make doDelete the callback for this form's onsubmit.
     When the user submits the form, doDelete will be called.
     If doDelete returns false, the form will not be submitted
     and the user will remain on the current page. -->
<form onsubmit="doDelete" ...

Comments

0

I assume your HTML is:

<form method="POST" action="page.php" onsubmit="return doDelete()">
   ...
</form>

And your javascript should then be something like:

function doDelete() {
   //if (THIS FAILS!()-STAY ON PAGE!) {
      // do something here
      return (confirm("Are you sure you want to update and then delete this hot part?"));
   //}
}

Something like this you mean? You can simply return the outcome of the confirm() method instead of doing an if-else.

I don't understand what's with the THIS FAILS though. Is this just a general remark of you? If so, leave it out... ;-)

2 Comments

inline event handlers are an anti pattern
It is exactly how he is making use of it though if you read his question. So don't shoot the messenger.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.