Magento – Open popup onClick of anchor tag

magento2

I need to open a popup on click of anchor link. I followed this (How to open popup when <a> tag onClick?) answer but the issue is that i have to add script inside .phtml file.

I want to include the script in .js file and then use it. How can i achieve this?

I have already tried by adding inside Vendor/Theme/web/js/custom.js but then the pop up is not getting displayed.

The second solution in the (How to open popup when <a> tag onClick?) link is not working for me.

Can some guide me towards this?

Best Answer

Please add the following to show popup:

<div class="view-popup"  onclick="show_popup()">
    <span>click to view popup</span>
</div>

whatever you want to show in popup,

<div id="popup-wrapper" style="display:none;" >
    <div class="popup-content">
        popup content popup content
    </div>
</div>

Add below popup script:

<script type="text/javascript">
    require([
        "jquery",
        'Magento_Ui/js/modal/confirm',
        'mage/storage',
        "jquery/ui",
        "jquery/validate",
        "mage/translate",
        "mage/mage"

    ], function ($, confirm, storage) {
        window.show_popup = function () {
            var options1 = {
                type: 'popup',
                responsive: true,
                clickableOverlay: false,
                title: 'Size Chart',
                buttons: [{

                }]
            };
            $("#popup-wrapper").show();
            $("#popup-wrapper").modal(options1).modal('openModal');
        }
    });
</script>

You can use Anchor tag instead of span.

clear the cache and check..

Related Topic