Search code examples
jqueryjquery-uijquery-dialog

JQuery Dialog: Check calling elements in beforeclose handler


I have a JQuery Dialog which has beforeclose handler method that refreshes the parent window:

$( "#dialog" ).dialog({
    beforeclose: function(event, ui) { 
       refreshParent(); 
    } 
});

The dialog once opened, can be closed from multiple location. Each time the dialog is closed, the refreshParent() method is called.

Now I need to check whether in certain conditions if dialog is closed, the refreshParent() should not be called.

One way of doing this would be to put a global flag like below:

$( "#dialog" ).dialog({
    beforeclose: function(event, ui) { 
       if(doRefreshParent)
           refreshParent(); 
    } 
});

And set doRefreshParent to true or false before calling dialog close. But this does not seem to be a good approach.

Is there any way we can check within beforeclose handler from where the dialog close got called(i.e the id of the element that got clicked, because of which dialog close happened). So that we can then control the invocation of refreshParent().


Solution

  • For recognition of the parent element, you can simply use a class to differentiate. A simple effect method. You can find a live example

    The main dialog configuration::

    jQuery("#dialog").dialog({
        autoOpen: false,
    
        modal: true,
    
        open: function() {
    
            var id = $('.parentelement').attr('id');
            $("#dialog").html("The parent id is::"+id);
            //alert("the id is:" + id);          
        },
         close: function(){
                $('.parentelement').removeClass('parentelement'); 
        }
    });