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.');}
<form action="http://www.google.com/search"><input type="text" name="q" /><input type="submit" value="Go"onclick="return confirm('Are you sure you want to search Google?')"/></form>
<!-- hyperlink example --><a href="http://www.example.com" data-confirm="Are you sure you want to load this URL?">Anchor</a>
<!-- button example --><button type="button" data-confirm="Are you sure you want to click the button?">Button</button>
<!-- form example --><form action="http://www.example.com" data-confirm="Are you sure you want to submit the form?"><button type="submit">Submit</button></form>
<!-- select option example --><select><option>Select an option:</option><option data-confirm="Are you want to select this option?">Here</option></select>
<script>function doSomething(){document.getElementById('id_confrmdiv').style.display="block"; //this is the replace of this line
document.getElementById('id_truebtn').onclick = function(){// Do your delete operationalert('true');};document.getElementById('id_falsebtn').onclick = function(){alert('false');return false;};}</script>
$("input[name='savedata']").click(function(e){var r = confirm("Are you sure you want to save now?");
//cancel clicked : stop button default actionif (r === false) {return false;}
//action continues, saves in database, no need for more code
});
// Call function when show dialog btn is clickeddocument.getElementById("btn-show-dialog").onclick = function(){show_dialog()};var overlayme = document.getElementById("dialog-container");
function show_dialog() {/* A function to show the dialog window */overlayme.style.display = "block";}
// If confirm btn is clicked , the function confim() is executeddocument.getElementById("confirm").onclick = function(){confirm()};function confirm() {/* code executed if confirm is clicked */overlayme.style.display = "none";}
// If cancel btn is clicked , the function cancel() is executeddocument.getElementById("cancel").onclick = function(){cancel()};function cancel() {/* code executed if cancel is clicked */overlayme.style.display = "none";}
<div id="content_1" class="content_dialog"><p>Lorem ipsum dolor sit amet. Aliquam erat volutpat. Maecenas non tortor nulla, non malesuada velit.</p><p>Aliquam erat volutpat. Maecenas non tortor nulla, non malesuada velit. Nullam felis tellus, tristique nec egestas in, luctus sed diam. Suspendisse potenti.</p></div>
<button id="btn-show-dialog">Ok</button>
<div class="overlay" id="dialog-container"><div class="popup"><p>This will be saved. Continue ?</p><div class="text-right"><button class="dialog-btn btn-cancel" id="cancel">Cancel</button><button class="dialog-btn btn-primary" id="confirm">Ok</button></div></div></div>
document.getElementById('test').addEventListener('click', test);
function test() {xdialog.confirm('Are you sure?', function() {// do work here if ok/yes selected...console.info('Done!');}, {style: 'width:420px;font-size:0.8rem;',buttons: {ok: 'yes text',cancel: 'no text'},oncancel: function() {console.warn('Cancelled!');}});}
const dialogEl = document.querySelector("dialog");const openDialog = document.querySelector("button.open-dialog");const yesBtn = document.querySelector(".yes");const noBtn = document.querySelector(".no");const result = document.querySelector(".result");
openDialog.addEventListener("click", () => {dialogEl.showModal();});
yesBtn.addEventListener("click", () => {// Below line can be replaced by your DB queryresult.textContent = "This could have been your DB query";dialogEl.close();});
noBtn.addEventListener("click", () => {result.textContent = "";dialogEl.close();});
<!DOCTYPE html><html>
<style>.modal_dialog{box-sizing: border-box;background-color: #ededed;border-radius: 5px;border: 0.5px solid #ccc;font-family: sans-serif;left: 30%;margin-left: -50px;padding: 15px 10px 10px 5px;position: fixed;text-align: center;width: 320px;}
</style>
<script src="./CustomModalDialog.js"></script>
<script>var gCustomModalDialog = null;
/** this could be static html from the page in an "invisible" state */function generateDynamicCustomDialogHtml(){
var html = "";html += '<div id="custom_modal_dialog" class="modal_dialog">';html += 'Name: <input id="name" placeholder="Name"></input><br><br>';html += '<button id="okay_button">OK</button>';html += '<button id="cancel_button">Cancel</button>';html += '</div>';return html;}
function onModalDialogOkayPressed(event) {
var name = document.getElementById("name");alert("Name entered: "+name.value);}
function onModalDialogCancelPressed(event) {
gCustomModalDialog.hide();}
function setupCustomModalDialog() {
var html = generateDynamicCustomDialogHtml();gCustomModalDialog = new CustomModalDialog(html, "okay_button", "cancel_button","modal_position", onModalDialogOkayPressed, onModalDialogCancelPressed);}
function showCustomModalDialog() {
if (! gCustomModalDialog) {setupCustomModalDialog();}gCustomModalDialog.show();gCustomModalDialog.setFocus("name");}
</script>
<body>
<button onclick="showCustomModalDialog(this)">Show Dialog</button><br>Some content<div id="modal_position"></div>Some additional content
</body>
</html>
CustomModalDialog.js:
/** Encapsulates a custom modal dialog in pure JS*/class CustomModalDialog {
/*** Constructs the modal content* @param htmlContent - content of the HTML dialog to show* @param okayControlElementId - elementId of the okay button, image or control* @param cancelControlElementId - elementId of the cancel button, image or control* @param insertionElementId - elementId of the <div> or whatever tag to* insert the html at within the document* @param callbackOnOkay - method to invoke when the okay button or control is clicked.* @param callbackOnCancel - method to invoke when the cancel button or control is clicked.* @param callbackTag (optional) - to allow object to be passed to the callbackOnOkay* or callbackOnCancel methods when they're invoked.*/constructor(htmlContent, okayControlElementId, cancelControlElementId, insertionElementId,callbackOnOkay, callbackOnCancel, callbackTag) {
this.htmlContent = htmlContent;this.okayControlElementId = okayControlElementId;this.cancelControlElementId = cancelControlElementId;this.insertionElementId = insertionElementId;this.callbackOnOkay = callbackOnOkay;this.callbackOnCancel = callbackOnCancel;this.callbackTag = callbackTag;}
/** shows the custom modal dialog */show() {// must insert the HTML into the page before searching for ok/cancel buttonsvar insertPoint = document.getElementById(this.insertionElementId);insertPoint.innerHTML = this.htmlContent;
var okayControl = document.getElementById(this.okayControlElementId);var cancelControl = document.getElementById(this.cancelControlElementId);
okayControl.addEventListener('click', event => {this.callbackOnOkay(event, insertPoint, this.callbackTag);});cancelControl.addEventListener('click', event => {this.callbackOnCancel(event, insertPoint, this.callbackTag);});} // end: method
/** hide the custom modal dialog */hide() {var insertPoint = document.getElementById(this.insertionElementId);var okayControl = document.getElementById(this.okayControlElementId);var cancelControl = document.getElementById(this.cancelControlElementId);
insertPoint.innerHTML = "";
okayControl.removeEventListener('click',this.callbackOnOkay,false);cancelControl.removeEventListener('click',this.callbackOnCancel,false);} // end: method
/** sets the focus to given element id*/setFocus(elementId) {
var focusElement = document.getElementById(elementId);focusElement.focus();if (typeof focusElementstr === "HTMLInputElement")focusElement.select();}
} // end: class