You can use the confirm()
method to show a confirmation message before deleting an item using JavaScript.
Here is an example of how you can use it in a delete button’s onclick
event:
<button onclick="if(confirm('Are you sure you want to delete this item?')){ deleteItem(); }">Delete</button>
This will display a confirmation dialog box with the specified message and two buttons: “OK” and “Cancel”. If the user clicks “OK”, the deleteItem() function will be executed, otherwise, nothing will happen.
You can replace deleteItem() function with your own function which will be used to delete the item.
You can also use the window.confirm()
function, which will work in the same way.
<button onclick="if(window.confirm('Are you sure you want to delete this item?')){ deleteItem(); }">Delete</button>
Also Read:
- What Is a Web Worker In JavaScript?
- How To Sort an Array of Strings In JavaScript?
- ECMAScript vs JavaScript
- What Is Strict Mode In JavaScript?
- Arrow Functions In JavaScript
- Difference Between Node.js and AngularJS With Example
- Difference Between == And === In JavaScript
- What Is JSON In JavaScript
- How To Go Back To Previous Page In JavaScript?
- How To Detect A Mobile Device With JavaScript?
- How To Close The Current Tab In A Browser Window Using JavaScript?
- How To Convert Input Text To Uppercase While Typing Using JavaScript?
- How To Detect Browser or Tab Closing In JavaScript?
- How To Get Hash Value From URL Using JavaScript?
- How To Get The Name, Size, And Type Of A File In JavaScript?
- Run JavaScript From The Command Line