-2

I have tried window.confirm on a submit button saying "Are you sure" but even if I click on no it submits itself? Is this the wrong way of using it?

<button type="submit" class="btn btn-primary" onclick="window.confirm('Are you sure you want to transfer to user?')">
Transfer
</button>
Rohan
  • 13,308
  • 21
  • 81
  • 154
  • Check it out http://stackoverflow.com/questions/6515502/javascript-form-submit-confirm-or-cancel-submission-dialog-box – Enzo Zapata Nov 27 '14 at 07:57
  • First of all you will need to show your code. Second, learn what `confirm` does: https://developer.mozilla.org/en-US/docs/Web/API/Window.confirm – Derek 朕會功夫 Nov 27 '14 at 07:57

3 Answers3

2

confirm returns a boolean. To make it work you will need to add return in the front:

return confirm(...);

http://jsfiddle.net/fu5LuLmx/

Instead of attaching to the click event, you might also want to consider attaching it to the submit event of the form, such that this piece of code would execute whenever the form is being submitted, not only when clicking this button.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
2

use this

<form onsubmit="return confirm('Are you sure you want to transfer to user?')">

</form>
Enzo Zapata
  • 171
  • 1
  • 7
1

You have to prevent from submitting so remove the onclick event, and add a onsubmit event on the form

<form name="form" id="myForm" onsubmit="return ask();">

<script>
function ask() {
    if  (window.confirm('Are you sure you want to transfer to user?')) {
        return true;
    } else {
        return false;
    }
}
</script>
Maxime
  • 1,332
  • 1
  • 15
  • 43
  • Won't do anything because you don't return the function's return value in the onsubmit handler. – JJJ Nov 27 '14 at 08:09