-1

When I click on "delete post" on StackOverflow it brings up a window on the screen asking if I want to go ahead or if I want to cancel.

How can I do that in Javascript so that it will work in both a computer and on an IPad?

henrikmerlander
  • 1,554
  • 13
  • 20
Alan2
  • 23,493
  • 79
  • 256
  • 450

3 Answers3

2

You can achieve it by using confirm, use the code as below:

<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
confirm("Press a button!");
}
</script>

The above example is very basic, if you some designed confirm box then please check craftpip's jquery-confirm Hope you like it.

Boniface Pereira
  • 188
  • 2
  • 11
Ram Singh
  • 6,664
  • 35
  • 100
  • 166
1

You can use confirm for this. It will return true or false depending on the user's action. Example:

var result = confirm("Please confirm this action!")
if(result)
    //The user confirmed the action
else
    //The user declined the action
henrikmerlander
  • 1,554
  • 13
  • 20
-1

As stated by others, use the confirm method.

You can get the callback from confirm like this:

confirm("Are you sure you want to do this?", function(result) {
if(result)
    doSomethingPositive();
else
    doSomethingNegative();
});
henrik123
  • 1,605
  • 13
  • 19
  • 1
    1) don't repeat answers other people have already given unless you have something to add. 2) this is not how you use `confirm()` –  Mar 17 '16 at 13:02
  • @yourmom whats wrong with this method? When I created my answer there were no examples with callback inovked – henrik123 Mar 17 '16 at 13:11
  • 2
    The confirm method does not have any callbacks. It simply returns true or false depending on user action. – henrikmerlander Mar 17 '16 at 13:15