Magento – way to completely destroy a modal in Magento 2

magento-2.1modal

I have created a modal with some dynamic content (input fields) and a save button to save input field data in database and I'm opening that modal on a button click.

Now when I fill all the input fields and save it or when I close the modal by pressing the "X" icon at top right without saving, the data in the modal persists and doesn't get cleared when I reopen that modal using button click.

Is there a way to completely destroy that modal and create a fresh one on each button click?

Best Answer

I dont think there is any destroy method in modal.js. If you want to clear input, reload the modal or whatever you want to do, you can implement closed event and do your thing when the modal is closing.

http://devdocs.magento.com/guides/v2.0/javascript-dev-guide/widgets/widget_modal.html#modal_closed

myModal.modal({
                    innerScroll: true,
                    modalClass: '_my-box',
                    buttons: [],
                    closed: function () {
                        alert('cleaning up');
                    }
                }).trigger('openModal');

The modals are based on Ui widget and that should implement _destroy() method that could also help you if the above doesnt work for any reason.

https://api.jqueryui.com/jquery.widget/#method-_destroy

Related Topic