A confirmation dialog asks the user to approve or cancel an action before it continues. This example uses jQuery UI Dialog and passes the result to a callback.
$( "#confirm-dialog" ).dialog({
autoOpen: false,
modal: true,
resizable: false,
width: 400,
buttons: {
Yes: function () {
$( this ).dialog( "close" );
handleConfirmation( true );
},
No: function () {
$( this ).dialog( "close" );
handleConfirmation( false );
}
}
});
function handleConfirmation( confirmed ) {
$( "#confirm-result" ).text(
confirmed
? "You clicked Yes."
: "You clicked No."
);
}
$( "#confirm-open" ).on( "click", function () {
$( "#confirm-message" ).text(
"Confirm to submit?"
);
$( "#confirm-dialog" ).dialog( "open" );
});
Original confirmation-box demo →
jQuery UI Dialog tutorial · click events
$("#dialog-window").dialog({
resizable: false,
autoOpen: false,
modal: true,
title: "Confirm Box",
height: 250,
width: 400,
buttons: {
"Yes": function () {
$(this).dialog('close');
callback(true);
},
"No": function () {
$(this).dialog('close');
callback(false);
}
}
});function callback(value) {
if (value) {
$('#display').html('You clicked Yes button')
}// end of if (value )
else{
// code to execute if false
$('#display').html('You clicked No button')
}
} // end of callback function$("#b1").click(function(){
$("#dialog-window").html("Confirm to Submit ...");
$( "#dialog-window" ).dialog( "open" );
})
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.