1

I need to run a custom function in a FancyBox data caption. My code looks like this:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
    <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.6/dist/jquery.fancybox.min.js" type="text/javascript"></script>
    <link href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.6/dist/jquery.fancybox.min.css" rel="stylesheet" type="text/css" />
        <a href="https://source.unsplash.com/juHayWuaaoQ/1500x1000" data-fancybox="gallery" data-caption="<button onclick=&quot; doSomething('data1', 'data2');&quot; type=&quot;submit&quot; class=&quot;btn btn-primary&quot;>Submit</button>">
<img src="https://source.unsplash.com/juHayWuaaoQ/240x160" alt="" />
</a>
    
<script>
function doSomething(data1, data2) {
    alert('Function works: '+data1+ ' and ' +data2 );
            //my stuff
 }
</script>

When I click on that button which is displayed in a FancyBox, I am getting an error: doSmething is not defined

If I create an additional button outside the FancyBox, then the function is working fine.

What am I missing?

After editing the snippet I found that the custom function works. Im confused.

EDITED: Everything works fine. Just my custom function was not loaded...

K. B.
  • 1,388
  • 2
  • 13
  • 25
  • 1
    See https://web.archive.org/web/20210325170940/https://fancyapps.com/fancybox/3/docs/#faq-2 – Janis Oct 13 '21 at 12:33

0 Answers0