jQuery UI Dialog events is used to trigger functions which support several rich sets of functions which can be used for trigger callback functions which are tabulated below.
Events |
Description |
create |
Is get into action when the dialog is being initialized. |
close |
Is get into action when the dialog is being closed. |
beforeClose |
Is get into action when the dialog is being to be closed. |
dragStart |
Is get into action when the dialog started being dragged. |
dragStop |
Is get into action when the dialog stopped being dragged. |
open |
Is get into action when the dialog is opened. |
resize |
Is get into action when the dialog is resized. |
resizeStop |
Is get into action when the dialog stops being resized. |
resize |
Is get into action when the dialog stops being resized. |
The jQuery UI Dialog event beforeClose allows you to get a notification once the dialog box is near to be closed. A good development observe is to permit the user to close the dialog, however, a user creates the user perform some task before the dialog is closed is as shown in below code.
[html]
<!DOCTYPE html>
<html>
<head>
<title>jQuery UI Dialog : beforeClose Event </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.3/themes/hot-sneaks/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-2.1.3.js"></script>
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<script>
$(document).ready(function() {
var closeable = false;
var delay = 5;
$('#demoDialog').dialog({
modal: true,
autoOpen: false,
beforeClose: function() {
return closeable;
},
open: function() {
var counter = delay;
var intID = setInterval(function() {
counter--;
$('#delayTime').text(counter);
if (counter == 0) {
clearInterval(intID)
closeable = true;
$('#demoDialog').dialog("close")
}
}, 1000)
}
})
$('button').click(function(e) {
$('#demoDialog').dialog("open")
})
});
</script>
</head>
<body>
<h1>jQuery UI Dialog : beforeClose</h1>
<div class="ui-widget">
<label for="userId">Username: </label><input id="user"/>
<label for="passwrd">Password: </label><input id="passwrd"/>
<button id="login">Login</button>
</div>
<div id="demoDialog" title="Incorrect Password">
Incorrect Password! Try Again in 5 Seconds. Time Left
<span id="delayTime">5</span> seconds.
</div>
</body>
</html>
[/html]
Result
By running the above code in a preferred browser user can get the following output as shown in below image.
If the user clicked on Log in button then a dialog box will appear as shown in below image.
Toggling The Dialog Box
The jQuery UI Dialog widget is toggled to open and close using methods of open, isOpen and close. The code below demonstrates, the method isOpen is used to check whether the dialog is open or not, depending on the results of an if-else statement, the dialog box is either opened or closed.
[html]
<!DOCTYPE html>
<html>
<head>
<title>jQuery UI Dialog : Toggling on Dialog Box</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.3/themes/hot-sneaks/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-2.1.3.js"></script>
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<script>
$(document).ready(function() {
var dialogOptions = {
autoOpen: false
};
$("#demoDialog").dialog(dialogOptions);
$("#toggleBtn").click(function() {
if(!$("#demoDialog").dialog("isOpen")) {
$("#demoDialog").dialog("open");
} else {
$("#demoDialog").dialog("close");
}
});
});
</script>
</head>
<body>
<h1>SPLESSONS Dialog Box</h1>
<div id="demoDialog" title="My Dialog Box">
Simple Programming Lessons
</div>
<button id="toggleBtn">Display Dialog</button>
</body>
</html>
[/html]
Result
By running the above code in a preferred browser user can get the following output as shown in below image.