I have a simple_dialog displaying information from a form in a custom module. I would like to be able to programmatically close the dialog in response to a click event on certain links within the dialog. However, I am unable to cause the dialog to close.

Here is a code excerpt. This code is executed as a result of an "onclick" attribute.

function sponsorLookupSelect(id) {
  jQuery.ajax({
    dataType: "json",
    url: '/sponsor-lookup/member-info?userId=' + id,
    success: function(data){
      var nameField = '#edit-field-sponsor-name-und-0-value';
      var emailField = '#edit-field-sponsor-e-mail-und-0-email';

      // Set the sponsor name and email in the correct sponsor fields.
      // These are working.
      jQuery(nameField).val(data.name);
      jQuery(emailField).val(data.email);
      
      // This part is not working.
      jQuery('#simple-dialog-container').dialog('close'); 
    }
  });
}

What am I doing wrong?

Thanks!

Comments

drclaw’s picture

Hmm... I just tested closing a dialog programmatically and it should work with jQuery('#simple-dialog-container').dialog('close'); . Are you getting any kind of error output?

paulmckibben’s picture

Thanks for the quick response and for trying it out. Unfortunately, I'm not getting any error output. I'm not even able to get Firebug to stop at a breakpoint within that ajax callback. Could the fact that it is within an ajax callback have something to do with the problem?

pico34’s picture

I have the same problem I can not close the window properly.
I tried this:

// not working !
jQuery(document).find('div.ui-dialog').remove();
jQuery(document).find('div.ui-widget-overlay').remove();

This seems to work because the window disappears, but I can not open it again.
I hope to find a solution ...

// not working too
jQuery('#simple-dialog-container').dialog('close');
pico34’s picture

Status: Active » Fixed

The good way is :

jQuery('.ui-icon-closethick').trigger('click');

cf. http://drupal.stackexchange.com/questions/64897/how-do-i-programmaticall...

Yeah :)

drclaw’s picture

@paulmckibben: Is your AJAX callback doing any of the other stuff in it? (e.g. jQuery(nameField).val(data.name);)

paulmckibben’s picture

Yes, the other code in the callback is being executed (e.g. jQuery(nameField).val(data.name);). The nameField is in the main portion of the page, and not within the simpleDialog.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

minor fix to code example

philsward’s picture

Example of my code to accomplish this:

<div align="center">
 <li class="cta-button"><input onclick="jQuery('.ui-icon-closethick').trigger('click');" type="button" value="Close Window"></input></li>
</center>

Or, if you prefer an anchor link:

<div align="center">
 <li class="cta-button"><a href="#" onclick="jQuery('.ui-icon-closethick').trigger('click');">Close Window</a></li>
</center>