{"id":11901,"date":"2020-09-18T07:25:50","date_gmt":"2020-09-18T01:55:50","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=11901"},"modified":"2020-10-09T18:57:04","modified_gmt":"2020-10-09T13:27:04","slug":"magento-2-create-call-modal-popup-widget","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-create-call-modal-popup-widget\/","title":{"rendered":"Magento 2 &#8211; How to Create and Call Modal Popup Widget"},"content":{"rendered":"<p>In this post I am going to explain how to create and call modal popup widget in Magento 2.<\/p>\n<p>First, you need to create a &#8220;mymodal.js&#8221; file at below path.<\/p>\n<p><strong>Path: app\/code\/&lt;vendor&gt;\/&lt;module&gt;\/view\/frontend\/web\/js\/mymodal.js<\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">define([\r\n    'jquery',\r\n    'Magento_Ui\/js\/modal\/modal'\r\n], function(\r\n    $,\r\n    modal\r\n) {\r\n    var options = {\r\n        type: 'popup',\r\n        responsive: true,\r\n        innerScroll: true,\r\n        buttons: [{\r\n            text: $.mage.__('Continue'),\r\n            class: 'my-modal',\r\n            click: function () {\r\n                this.closeModal();\r\n            }\r\n        }]\r\n    };\r\n\r\n    var popup = modal(options, $('#modal-content'));\r\n    $(\"#open-btn\").on('click',function(){\r\n        $(\"#modal-content\").modal(\"openModal\");\r\n    });\r\n});\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>After creating js file, create a &#8220;requirejs-config.js&#8221; at file below path and add\/call js in it.<\/p>\n<p><strong>Path: app\/code\/&lt;vendor&gt;\/&lt;module&gt;\/view\/frontend\/requirejs-config.js<\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">var config = {\r\n    map: {\r\n        '*': {\r\n            mymodal: 'Ht_Mymodule\/js\/mymodal'\r\n        }\r\n    }\r\n};<\/pre>\n<p>Now, add below code in your phtml file.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">&lt;div data-mage-init='{\"mymodal\": {}}'&gt;\r\n    &lt;a class=\"action primary\" href=\"#\" id=\"open-btn\"&gt;Open Modal&lt;\/a&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;div style=\"display: none\"&gt;\r\n    &lt;div id=\"modal-content\" &gt;\r\n        Modal content goes here...\r\n    &lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>Thats it. Enjoy Magento 2!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to create and call modal popup widget in Magento 2. First, you need to create a &#8220;mymodal.js&#8221; file at below path. Path: app\/code\/&lt;vendor&gt;\/&lt;module&gt;\/view\/frontend\/web\/js\/mymodal.js &nbsp; define([ &#8216;jquery&#8217;, &#8216;Magento_Ui\/js\/modal\/modal&#8217; ], function( $, modal ) { var options = { type: &#8216;popup&#8217;, responsive: true, innerScroll: true, buttons: [{ text:&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-create-call-modal-popup-widget\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":11927,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[181,59,2138],"tags":[729,9,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11901"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=11901"}],"version-history":[{"count":1,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11901\/revisions"}],"predecessor-version":[{"id":11902,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11901\/revisions\/11902"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/11927"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=11901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=11901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=11901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}