2

I'm having trouble attempting to dynamically add images, since lightbox2 (by lokesh dakar) is initialized after document load by existent html coded images and it doesn't parse the document anymore after the first time.

If i try to add new images with javascript (appending them in the body for example) they are not added to the lighbox groups.

Documentation in official website doesn't seems to describe any scripted method

Anyone with experience can help me? solution with jquery are really welcome, but i can handle well vanilla js too.


Code sample:

HTML

<body>
 <div id="container">
  <a href="images/image-2.jpg" data-lightbox="roadtrip">Image #2</a>
  <a href="images/image-3.jpg" data-lightbox="roadtrip">Image #3</a>
  <a href="images/image-4.jpg" data-lightbox="roadtrip">Image #4</a>
 </div>
 <!-- where 'Image #X' = <img src="images/thumb/image-X.jpg>" -->
 <div id="loadmore">load more images</div>
 <script src="path/to/lightbox.js"></script>
 <script>
  /* see below for the script */
 </script>
</body>

javascript

$(function(){
  $('#loadmore').click(function(){
   //ajax request for more images.
   //load them with all the needed properties...
   //then put them into the container:
   var IMG = $('<a href="images/image-10.jpg" data-lightbox="roadtrip">'+
       '<img src="images/small/image-10.jpg">'+
     '</a>');
   //magic here... add IMG to roadtrip lightbox group!!!
   //probably something like lightbox.groups['roadtrip'].add(IMG)
   //but i'm only speculating
   $('#container').append(IMG)
  });
})
2
  • Could you add your code snippet here. Commented Dec 24, 2015 at 15:40
  • @JitendraKhatri edit done! Commented Dec 25, 2015 at 14:57

3 Answers 3

8

After a couple of hours spent on testing and reading the code, I came up with this magic code:

//set album values
IMG = $('<a href="...">')
    .attr('data-lightbox','roadtrip')
    .append('<img src="...">')
//lightbox open:
IMG.click(function(){
    lightbox.start($(this));
    return false;
})

some helpful tips:

  • don't set data with $(...).data('lightbox','group'), since lightbox uses the selector $('a[data-lightbox]') each time a link is clicked. Instead, you should use $(...).attr('lightbox','group').

  • lightbox.album is a temporary variable which you don't need to deal with.

Sign up to request clarification or add additional context in comments.

5 Comments

How are you able to access the lightbox variable? I'm getting a "not defined" error.
first: assure you are using lightbox 2 (the latest). this script MUST be placed AFTER lightbox2 has loaded. so place your script after the <script src="lightbox2.js"></script> inclusion.
Thanks for the help! I gave up on calling Lightbox2 directly. It seems I was having difficulty with Lightbox's internal state--things inside Lightbox were causing the "undefined" errors.
lightbox.start($(this)); this was it for me. :)
@NereoCostacurta, I am trying to show 2 dynamic images on click. on function click, I set attribute of data-lightbox and href of <a>. But it is only showing the second image i.e. last image, not both!!
2

Im a couple of years late with a solution to this, but hopefully it will help others in the future.

You can re-initialize Lightbox by calling lightbox.init() after adding your images.

For example:

var src = "https://via.placeholder.com/150"
$(`#photo-container`)
.append(`
    <a href="${src}" data-lightbox="album">
        <img src="${src}" width="100">
    </a>
`);

lightbox.init();

Be sure to include lightbox and jQuery before your script

Comments

1

To add existing img tags to be lightboxified on their own (without gallery functionality) one could use something like:

$('img')
    .wrap(function(index) {
        return '<a href="'+$(this).attr('src')+'" data-lightbox="image-'+index+'"></a>';
    });

Just add it BEFORE inserting the lightbox.js

1 Comment

well, thank you for the proposal @Storrm! but in the question is explicitly required the "dynamic" addition, so lightbox should be already operative... and added image (or existing ones not already lightboxified) should trigger LB on click. this is achived ONLY by the second part of my accepted answer: IMG.click(function(){ lighbox.start($(this)); return false; })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.