12

In Magnific Popup, I want to get an attribute in the link that is clicked and use it in a callback function (using callbacks: open) to make some changes in the DOM.

How can I do this? For example, in the code below, it should return 'it works' to console. Instead it prints 'doesnt work'. Please help!!

<a href="#test-popup" class="open-popup-link" myatt="hello">Show inline popup</a>

<script src="jquery.magnetic.custom.js"></script>

<script>

    $(document).ready(function() {
      $('.open-popup-link').magnificPopup({
        type:'inline',
        midClick: true,
        callbacks: {
          open: function() {

            if ($(this).attr('myatt')=="hello") 
            { 
              // do something 
              console.log("it works");
            }
            else
            {
              console.log("doesnt work");
            }

          },
          close: function() {

          }
        }

      });
    });

</script>

<div id="test-popup" class="white-popup mfp-hide">
  Popup content
</div>
Imme22009
  • 3,992
  • 7
  • 31
  • 47

6 Answers6

15

For Magnific Popup v0.9.8

var magnificPopup = $.magnificPopup.instance,
              cur = magnificPopup.st.el;
console.log(cur.attr('myatt'));
Konpaka
  • 322
  • 3
  • 11
6

First, i recommend to you to use the data attribute ( data-custom="foo" ) or a known attribute.

HTML :

  <a href="photo.jpg" class="popup" data-custom="dsaad">Do it!</a>
  <a href="photo.png" class="popup" data-custom="mycustomdata">Do it!</a>

jQuery :

$('.popup').magnificPopup({
  type : 'image',
  callbacks : {
    open : function(){
      var mp = $.magnificPopup.instance,
          t = $(mp.currItem.el[0]);

      console.log( t.data('custom') );
    }
  }
});

I do not know if a better way exists. Actually you can read their documentation about public methods and you will see there. I tested the code above and everything works fine :)

stefanz
  • 1,316
  • 13
  • 23
  • 2
    To note the above only works for older versions of Magnific Popup. If you are using anything past version 0.9.8 please see @Konpaka answer below. – Styledev May 23 '14 at 17:15
5

For v. 0.9.9:

this.currItem.el

user3364436
  • 51
  • 1
  • 1
0
// "item.el" is a target DOM element (if present)
// "item.src" is a source that you may modify
open: function(item) {}

and use data-attributes, to example data-myatt - that get:

$(this).data('myatt')
maximkou
  • 5,252
  • 1
  • 20
  • 41
0

The clicked element can be accessed within the callback using:

this.st.el

Inside the callback, "this" refers to $.magnificPopup.instance.

Under public properties:

"magnificPopup.st.el // Target clicked element that opened popup (works if popup is initialized from DOM element)"

jwinn
  • 1,135
  • 3
  • 14
  • 30
0

Also, inside open: function(item) {}, this.content might help.. It will return the div of the content being shown. useful with the change: function () {} as well. Hope it helps someone like me.

StinkyCat
  • 1,236
  • 1
  • 17
  • 31