I'd like to be able to detect the (x) close button of a jQuery UI Dialogue being clicked, but I don't want to use the dialogclose
/ dialogbeforeclose
events (since I believe these will fire regardless of how the dialog was closed).
I tried $(".ui-dialog-titlebar-close").live("click")
, but that doesn't seem to work.
How can I do this?
Sample code: (the debugger doesn't fire up when the dialogue is closed).
<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.js"></script>
<script>
$(document).ready(function() {
$("#dialog").dialog();
$(".ui-dialog-titlebar-close").live("click", function() {
debugger; // ** clicking the close button doesn't get to here.**
});
});
</script>
</head>
<div id="dialog" title="Dialog Title">I'm in a dialog</div>
</body>
</html>
You do not want to do this via
.live
etc as you'll end up binding to the X of every dialog you create. You want to bind to a specific dialog's X for a specific purpose, so...Note Before you read on, note that this works perfectly but is overly complex. Kris Ivanov has posted a more correct, more concise, more appropriate answer. End Note
In the dialog's open method, check to see if you've already bound the click to the 'X'. If not, flag that you have and then find your instance's 'X' and bind it:
You need the check for whether it has been bound because
open
runs every time the dialog opens, so multiple opens would rebind the same functionality over and over without it.Demo: http://jsfiddle.net/XM2FH/
Really good question
It's working if You use only the click
But i'm sure there's a reason for the live ?
I'll continue looking
And why you don't want to use this ?
you could do exactly what JAAulde suggested, or avoiding tracking binding and use the
create
event:I know this is an old question, and the OP said he didn't want to use beforeClose, but the reason was because it always fires, even for things other than the X. However, I noticed that the techniques here don't allow me to prevent a dialog from closing (I want a confirm window to open if there are unsaved changes). If we Ithe techniques here, use beforeClose, we can achieve the desired result, but make it cancellable. I used:
Thought it might help someone else!