0

I am using Magnific Popup for my gallery. When you click on a thumbnail, it popups the image. I am trying to add a "Download image" button inside the popup. I have the HTML snippet displaying fine, but the href of the download link needs to be populated with the img url.

Here is the JS code:

markup: '<div class="mfp-figure">'+
                '<div class="mfp-close"></div>'+
                '<div class="download-img"><a href="#">Download image</a></div>'+
                '<figure>'+
                    '<div class="mfp-img"></div>'+
                    '<figcaption>'+
                        '<div class="mfp-bottom-bar">'+
                            '<div class="mfp-title"></div>'+
                            '<div class="mfp-counter"></div>'+
                        '</div>'+
                    '</figcaption>'+
                '</figure>'+
            '</div>',

How do I put the image src variable in the anchor's href?

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
Garrettj944
  • 325
  • 3
  • 17

3 Answers3

2

If you check the Magnific Popup API you will see that it provides a callback for the event markupParse

So add this to your Magnific Popup options

callbacks: {
      markupParse: function(template, values, item) {
       template.find('.download-img a').prop('href',item.src);
      }
    }

Full demo at http://codepen.io/gpetrioli/pen/reoqQv

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
1

Just put the variable out of quotes.

markup: '<div class="mfp-figure">'+
                '<div class="mfp-close"></div>'+
                '<div class="download-img"><a href="' + yourVar + '">Download image</a></div>'+
                '<figure>'+
                    '<div class="mfp-img"></div>'+
                    '<figcaption>'+
                        '<div class="mfp-bottom-bar">'+
                            '<div class="mfp-title"></div>'+
                            '<div class="mfp-counter"></div>'+
                        '</div>'+
                    '</figcaption>'+
                '</figure>'+
            '</div>',
Jakub Rożek
  • 2,110
  • 11
  • 12
0

The following link shows you a number of ways you can use href some are good and others are bad. Have a look at https://stackoverflow.com/a/11348403/886393

specifically the answer by username demp.

Thanks, Paras

Community
  • 1
  • 1
pparas
  • 549
  • 4
  • 13
  • I am trying to echo a js variable, not run a function when they click. If you are willing to look into the code, I can pay you – Garrettj944 May 05 '16 at 20:31
  • post the code here. I can help you out and so can the community. I do this for practice. I do not want to get paid. – pparas May 05 '16 at 20:39
  • @Jakub is looking into it, you can too if you'd like. See my comment his answer :) – Garrettj944 May 05 '16 at 20:43