-1

Jquery popup close icon click event is not working?

http://jsfiddle.net/ymssceqv/1888/

JS:

//Set up the dialog box
$("#myDialog").dialog({
autoOpen  : false,
modal     : true,
title     : "A Dialog Box" 
});

//Open the dialog box when the button is clicked.
$('#clickMe').click(function() {
$("#myDialog").dialog("open");
});

$(document).on('click', '#myDialog .ui-dialog-titlebar-close', function (e) { 
  e.preventDefault()
  alert('you clicked the x!');
});
Kanaka k
  • 95
  • 2
  • 12

2 Answers2

0

You can set z-index for below divs. So that close event will be called on close button.

.ui-front{
   z-index: 1;
}
#myDialog{
   z-index: 2;
}

You can use beforeClose property of the dialog box:

$("#myDialog").dialog({
    autoOpen  : false,
    modal     : true,
    title     : "A Dialog Box",
    beforeClose: function () {
        customFunction();
    }
});

function customFunction() {
    alert('Custom function called');
}
0

I don't know how to modify the behaviour of the default close button. But a workaround is hiding that button and adding a new one:

$("#myDialog").dialog({
    dialogClass: "no-close",   // Add a class to hide default close button
    buttons: [    //Add your own button
      {
        text: "New button",
        click: function() {    //Add your own click event
          test();
        }
      }
    ],
    autoOpen: false,
    modal: true,
    title: "A Dialog Box"
});

//Open the dialog box when the button is clicked.
$('#clickMe').click(function () {
    $("#myDialog").dialog("open");
});


function test() {
    alert("close icon clicked");
    //Some function script...............
}
/* Hide default button */
.no-close .ui-dialog-titlebar-close {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<div id="myDialog">
    This is the content of my modal dialog box
    <input type="text" id="myTextBox" />
</div>

<button id="clickMe">Click Me</button>

In this way you can have a button, but in a different position. You can play with css to move it if you like. Anyway, I don't understand why you want a dialog that doesn't close....

Damian Peralta
  • 1,846
  • 7
  • 11