How to show a confirm message before delete?
Kick-start a confirmation dialog using JavaScript's confirm()
function with the onclick
event:
Clicking 'OK' gives a green light to delete, and 'Cancel' hits the brakes.
Grasping the essence of user confirmation
Deleting is often irreversible. Providing a catch-net in form of a confirmation using the confirm()
function lets users take a second glance at their decision, potentially avoiding data disaster.
Directing user interaction
onclick
event is every web developer's trusty sidekick. Coupling it with the confirm()
function gives users an intuitive option to reconsider the delete command:
Staying cool with design
The confirm()
dialog is a native element, so it chillingly coexists with your existing page design without causing any stylistic havoc.
Drawing a line between HTML and JavaScript
Keeping HTML and JavaScript in their respective lanes makes for cleaner and neater coding. How about this:
And the HTML goes:
Mastering enhanced confirmations
Further beautify the user experience and code maintenance with some nifty techniques.
Using data attributes as secret messages
HTML5 data-*
attributes offer a neat way to tuck away confirmation messages in your HTML, resulting in clean JavaScript and well-separated functionality:
Then, summon the attribute from JavaScript:
Stopping the unstoppable event
For advanced delete scenarios, preventDefault()
function keeps things in check until user confirms the delete action:
Embracing all the browsers
Make sure your code is a friendly neighborhood web developer, supporting a wide variety of browsers - yes, even the prehistoric Internet Explorer!
Diving into custom solutions
When confirm()
begins to feel too vanilla, mix things up with some sophisticated approaches to take your application's usability a notch higher.
Brewing a custom modal
How about a custom-built modal that allows you to call the shots on styling and interaction, and doesn't give a hoot about ad-blockers?
In JavaScript, show and hide the modal as needed and handle button actions:
Catering to user preferences
Always walk a mile in the user's shoes. Make sure the confirmation step is no rocket science, and doesn't leave them guessing or grumbling.
Was this article helpful?