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?
Avoid inline JavaScript - changing the behaviour would mean editing every instance of the code, and it ain't pretty!
A much cleaner way is to use a data attribute on the element, such as
data-confirm="Your message here"
. My code below supports the following actions, including dynamically-generated elements:a
andbutton
clicksform
submitsoption
selectsjQuery:
HTML:
JSFiddle demo
This a full responsive solution using vanilla javascript :
This plugin can help you jquery-confirm easy to use
Use
confirm
instead of alert. This is the easiest way to achieve that functionality.Another way to do this:
You have to create custome confirmBox,it is not possible to change the buttons in the dialog displayed by the confirm function.
see this example: https://jsfiddle.net/kevalbhatt18/6uauqLn6/
Call it by your code:
Example: http://jsfiddle.net/kevalbhatt18/qwkzw3rg/127/
Script:
CSS: