You’re probably looking for confirm(), which displays a prompt and returns true or false based on what the user decided:
if (confirm('Are you sure you want to save this thing into the database?')) {
// Save it!
console.log('Thing was saved to the database.');
} else {
// Do nothing!
console.log('Thing was not saved to the database.');
}
Answer from s4y on Stack OverflowVideos
You’re probably looking for confirm(), which displays a prompt and returns true or false based on what the user decided:
if (confirm('Are you sure you want to save this thing into the database?')) {
// Save it!
console.log('Thing was saved to the database.');
} else {
// Do nothing!
console.log('Thing was not saved to the database.');
}
var answer = window.confirm("Save data?");
if (answer) {
//some code
}
else {
//some code
}
Use window.confirm instead of alert. This is the easiest way to achieve that functionality.
You can easily do it with a confirm onclick:
Copy<p id="accept-favor"><a title="Accept this Favor"
href="?wp_accept_favor=<?php comment_ID(); ?>"
onclick="return confirm('Are you sure you would like to accept this reply as your favor?');"
>Accept this Favor</a></p>
Though this will say OK/Cancel instead of Yes/No. If you really want Yes/No, you'll have to use a custom dialog.
You can write onclick="return confirm('Are you sure?');".
The confirm function shows an OK / Cancel dialog and returns true if the user clicked OK.
returning false from an onclick handler will cancel the default action of the click.
You basically just need to bootstrap myFunction, and also place the prompts inside the myFunction so they get called again if the confirmation is false.
eg.
function myFunction() {
var name = prompt('What is your name?');
var conf = confirm('Is your name: ' + name);
if (conf === true) {
alert("You pressed OK!");
} else {
alert("Input your correct name");
myFunction();
}
}
myFunction();
I have encapsulated your code a function and call the function again if the confirmation is no.
Here, I am passing an extra parameter, which confirms if the questions are asked first time. If it not asked first time, then it will show Input your correct name. also.
function askAndConfirm(isFirst){
var question="";
if(!isFirst) question = "Input your correct name. ";
var name = prompt(question + 'What is your name?');
var isConfirm = confirm('Is your name: ' + name);
if (isConfirm === true) {
alert("Hi! "+name);
} else {
askAndConfirm(false);
}
}
askAndConfirm(true);