-2

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!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

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.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • but ignoring the wrong class name, if he does this on just before

    tag should be work, or not?

    – Fiambre May 29 '14 at 13:26
  • It should, but I have seen some browsers not run the jQuery without the document ready handler. It's just good practice. – Jay Blanchard May 29 '14 at 13:27
  • In this case, lightbox waits till the document is ready so if you just add the click before the end body tag, lightbox will wait until after the script is loaded before initialising which is why it doesn't work the way the op has done it (as the click happens before lightbox is added to the link) – Pete May 29 '14 at 13:46
  • @JayBlanchard the class was a post type. The values are correct in the real script. – Ònno van Helsdingen May 29 '14 at 16:17
  • @Pete Where does the script part have to be written then? After the closing html tag or with a document ready function as Jay already said? – Ònno van Helsdingen May 29 '14 at 16:18
  • I see, so we don't have all of the information that we need to solve the issue? Can you tell us which lightbox you are using? Are there any errors in the browser's console? – Jay Blanchard May 29 '14 at 16:18
  • @JayBlanchard No errors. I am using the Lightbox v2.7.1 by Lokesh Dhakar. – Ònno van Helsdingen May 29 '14 at 16:26
  • @JayBlanchard I don't know what it was but after applying your code in my script and a xampp restart it suddenly does what it was supposed to do! Thanks dude! This works. – Ònno van Helsdingen May 29 '14 at 16:29