Fancybox 3 on page load with delay and set cookie not working

1.6k views Asked by At

I can not get this jQuery to work on page load? I use fancybox 3.

<script>
  function openFancybox() {
    setTimeout( function() {
      $('[data-fancybox data-src="#newsletterFancy"]').trigger('click'); 
    }, 20000);
  }

  $(document).ready(function() {
    var visited = $.cookie('visited');
    if (visited == 'yes') {
      return false;
    } else {
      openFancybox();
    }

    $.cookie('visited', 'yes', { expires: 7 });
    $('[data-fancybox data-src="#newsletterFancy"]').fancybox();
  });
</script>

I have also added this to my body tag: <body OnLoad="openFancybox()" class="body">

I basically have my pop up in a included file called newsletter.php. The link in my sidebar works fine when i click that. But i want to make it pop up and open on page load with a delay and also for it to set a cookie.

I have also included the cookie js as well:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script> 

This is the line I use in my sidebar link for it to open when you click it:

<a class="buttons" data-fancybox data-src="#newsletterFancy" href="javascript:;">Newsletter Subscribe</a>

Thanks

1

There are 1 answers

0
Janis On BEST ANSWER

You are simply not using valid selector. Replace

$('[data-fancybox data-src="#newsletterFancy"]')

with, for example:

$('[data-src="#newsletterFancy"]')