Search code examples
javascriptjqueryhreflightbox2

Open a lightbox link with simple jQuery


I have read a lot of answers and different questions but I can't find a clear answer to my problem.

I have this link in my html dom:

<a href="img/someImg.jpg" data-lightbox="groupName" data-title="LogoTitle" class="MyClass"> Title </a>

I have these two files in the head function:

<script src="script/js/jquery-1.11.0.min.js"></script>
<script src="script/js/lightbox.js"></script>

When the page is loaded it needs to open the link inside the lightbox and people should navigate in the lightbox through the image group that is defined with the data-lightbox.

I tried using this at the end of the body:

  <script>
    $(".logo").click();
  </script>
</body>

But no result :(.

Nothing happens. Page loads but the auto click doesn't do anything. I had it working but then Lightbox didn't open up. Instead the image of the link was loaded.

All other codes in jQuery work between the script tags.

Am I missing something here?

Please help or ask if things are not clear enough. I am a newbie here and try to play by the rules of the forum.

Thanks in advance!


Solution

  • There are a couple of problems. The link you posted has the class MyClass and you probably need a document ready handler -

    <script>
    $(document).ready(function() {
        $('.MyClass').click();
    });
    </script>
    

    Which lightbox are you using? There may be additional requirements that you need to implement.