snippetjavascriptCritical
How to create a dialog with “Ok” and “Cancel” options
Viewed 0 times
withhowcancelandoptionsdialogcreate
Problem
I am going to make a button to take an action and save the data into a database.
Once the user clicks on the button, I want a JavaScript alert to offer “yes” and “cancel” options. If the user selects “yes”, the data will be inserted into the database, otherwise no action will be taken.
How do I display such a dialog?
Once the user clicks on the button, I want a JavaScript alert to offer “yes” and “cancel” options. If the user selects “yes”, the data will be inserted into the database, otherwise no action will be taken.
How do I display such a dialog?
Solution
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.');
}Context
Stack Overflow Q#9334636, score: 1639
Revisions (0)
No revisions yet.