question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use Magnific Popup with AJAX-loaded content

See original GitHub issue

Hi,

I have some content that is loaded through AJAX. Magnific doesn’t work with that content because the event listeners fire on page load, but the the AJAX-loaded content is not yet inserted in the page at that point.

The only workaround for this that I know of is to use inline javascript on the AJAX-loaded content. I tried to do something like:

<a class="test-popup-link" href="path-to-image.jpg" onclick="magnificPopup({type: 'image'}).magnificPopup('open');">Open popup</a>

But unfortunately it doesn’t seem to work. I get ReferenceError: magnificPopup is not defined, even though the magnific-popup.js file is included in the page.

Any help on how to get this to work would be appreciated. Thanks!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

8reactions
jonyextenzcommented, Oct 16, 2020

use document as main selector, but if you think it’s too general, you can change it to any selector as long as it’s not included inside AJAX dom element,

and then put your own selector (selector inside AJAX dom element) to delegate options

$(document).magnificPopup({
    delegate: '.your-class-selector',
});

It works for me, no need to re initialize

2reactions
ragoutjecommented, Jun 21, 2017

On document load initialize mp as normal:

jQuery(function () {
    $(this).magnificPopup();
});

In your ajax call, trigger the binding of magnific popup after the ajax content is loaded:

$.ajax({
        type: 'get',
        url: '/get-ajax-content',
        success: function (data) {
            // Rebind modal events
            $(this).magnificPopup();
        }
    });

In your magnificPopup jQuery function:

(function ($) {
    $.fn.magnificPopup = function () {
        $('[data-modal]').magnificPopup({
            // options
        });
        
        return this;
    };
})(jQuery);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to pass data via the post method to magnific ...
I'm using magnific popup and ajax loading content into it and passing values to the ajax content by appending a query string to...
Read more >
Display Magnific Popup using AJAX on Page Load
Dear Sir/Madam I found a solution to display an Image on pageload ...
Read more >
Magnific Popup Documentation - Dmitry Semenov
The complete guide on how to use Magnific Popup - the open source responsive ... Popup has four types of content: image ,...
Read more >
Magnific Popup - loading content from external page via ajax ...
data = jQuery(mfpResponse.data).find('#gallery'); console.log('Ajax content loaded:', mfpResponse); }, ajaxContentAdded: function() { $('# ...
Read more >
Réinitialise magnific popup after a filtering operation - Toolset
Solution: The user has activated AJAX on the view, after the first-page load, any further AJAX calls will not rebuild the lightbox. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found