23

I want to close a Simplemodal window from a JavaScript function that gets called automatically after a form is submitted and the results recived (AJAX), using ASP.Net MVC. How do I close a jQuery Simplemodal?

I've opened it this way:

$("#popup").modal()
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

4 Answers4

66

You have 2 options:

1) Put the close class (simplemodal-close) on an element in your modal data and SimpleModal will automatically bind the close function to the click event on that element.

Taking the example above, you'd want:

<div id="foo" style="display:none">
  <p>Form HTML</p>
  <span class="simplemodal-close">Close</span>
</div>

2) When you want to close the dialog programatically, call:

$.modal.close();

HTH!

-Eric (SimpleModal author)

Eric Martin
  • 2,841
  • 2
  • 23
  • 23
  • Where is CDN for css of simple modal? I've found only CDN for *.js of simple modal: https://cdn.jsdelivr.net/simplemodal/1.4.4/jquery.simplemodal.min.js – FrenkyB Apr 04 '16 at 14:51
7

Just call close.

$("#popup").close();

If you're doing it for an ajax completion you need to add a callback. You may want to check for failure.

var foo = $("#popup").modal();

$.ajax({url:url, success:function(){
    foo.close();
}});
teedyay
  • 23,293
  • 19
  • 66
  • 73
gradbot
  • 13,732
  • 5
  • 36
  • 69
4

If none of the above solutions work, you can also try to trigger the click event of the close button (so long as it is placed inside the popup element).

$( ".simplemodal-close" ).trigger( "click" );

or failing that, with Javascript.

document.getElementById('closeModalButton').click();
milesholt
  • 161
  • 1
  • 9
1
$.modal.close();

or you can use following command

setTimeout('goto_previous_page()', 1000);
Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83