Search code examples
jquerymagnific-popup

If doesn't work inside a modal


It works fine if the HTML is inside the DOM. But I have an AJAX-modal (magnific Popup) and there the script doesn't work.

The class "no-touch" won't be removed, any ideas what I have to change that it works inside the modal as well?

$(document).ready(function() {
  win_w = $(window).width();
  win_h = $(window).height();
  if (/Mobi/i.test(navigator.userAgent)) {
    $(".mgu-profil-selection__food5, .mgu-profil-selection__food6").removeClass("no-touch");
  }
})


Solution

  • Your code is only executed on page load, you should also run this after the ajax request

    You can assign callbacks to your magnific Popup (see docs)

    function check() {
      win_w = $(window).width();
      win_h = $(window).height();
    
      if (/Mobi/i.test(navigator.userAgent)) {
        $(".mgu-profil-selection__food5, .mgu-profil-selection__food6").removeClass("no-touch");
      }
    }
    
    $(document).ready(function() {
      check();
    })
    
    
    $('.image-link').magnificPopup({
      type: 'image',
      callbacks: {
        ajaxContentAdded: function() {
          // Ajax content is loaded and appended to DOM
          // run check function again
          check();
        }
      }
    });