Popup modal closure on ajax respone

I have a popup modal that opens onclicks, I want this modal to be closed when I submit the form and get ajax success request. I tried using

success: function(data)
{
    //console.log('ticket added');
    this.closeModal();
}

But it does not work, can anyone figure this out. Thanks at Advance

<script>
require(
    [
       'jquery',
       'Magento_Ui/js/modal/modal'
    ],
    function($, modal) {
        var options = {
            type: 'popup',
            responsive: true,
            innerScroll: true,
            title: 'Test title',
            buttons: [{
                text: $.mage.__('Continue'),
                class: '',
                click: function () {
                    this.closeModal();
                }
            }]
         };

        var popup = modal(options, $('#events_popup'));
        $("#click-me").on('click',function(){ 
            $("#events_popup").modal("openModal");
        });
});
</script>

<div id="events_popup" style="display: none;" class="events_popup">
<script type="text/javascript">
    require(
        [
            'jquery'
        ],
    function($,modal) {
        $("#<?php echo $_product->getId()?>").submit(function(e) {

            $.ajax({
                type: "POST",
                url:  $(this).attr('action'),
                data: $("#<?php echo $_product->getId()?>").serialize(), 
                showLoader: true,
                success: function(data)
                {
                    //console.log('ticket added');
                    document.close();
                }
            });

            e.preventDefault(); 
        });
    }
); 

</script>
</div>
+4
source share
2 answers

You tried

$('#yourmodalid').modal('hide');
+1
source

I got a solution, we can destroy modal with the following line of code.

$('#events_popup').modal('closeModal');
+1
source

Source: https://habr.com/ru/post/1676633/


All Articles