1

I read many different posts about this matter but I can't solve my problem. I am trying to create a simple lightbox on dynamically generated content.

   $(document).ready(function() {
  $("body").on("click", "button", function() {
    $("button").removeClass("selected");
    $(this).addClass("selected");

    var flickrAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
    var animal = $(this).text();
    var flickrOptions = {
      tags : animal,
      format: "json"
    };
    var displayPhotos = function(data) {
      var photoHTML = "<ul>";
      $.each(data.items, function(i, photo) {
        photoHTML += '<li class="grid-25 tablet-grid-50">';
        photoHTML += '<a href="' + photo.link + '" class="image">';
        photoHTML += '<img src="' + photo.media.m + '" ></a></li>';
      });
      photoHTML += '</ul>';
      $('#photos').html(photoHTML);
    }
    $.getJSON(flickrAPI, flickrOptions, displayPhotos);

      var $overlay = $('<div id="overlay"></div>');
      var $image = $("<img>");
      var $caption = $("<p></p>");

      //An image to overlay
      $overlay.append($image);

      //A caption to overlay
      $overlay.append($caption);

      //Add overlay
      $("body").append($overlay);

      //Capture the click event on a link to an image
      $("#photos a").click(function(event){
        event.preventDefault();
        var imageLocation = $(this).attr("href");
        //Update overlay with the image linked in the link
        $image.attr("src", imageLocation);

        //Show the overlay.
        $overlay.show();

        //Get child's alt attribute and set caption
        var captionText = $(this).children("img").attr("alt");
        $caption.text(captionText);
      });

      //When overlay is clicked
      $overlay.click(function(){
        //Hide the overlay
        $overlay.hide();
      });
  });
});

Here is my complete code on jsfiddle

The click event after the AJAX call doesn't fire up. How can I solve this?

2
  • where its your click event i dont see it... Commented Jan 6, 2015 at 2:55
  • Please have a look at the complete code on jsfiddle: jsfiddle.net/wisd81/23gzbqdh/3 Commented Jan 6, 2015 at 2:57

1 Answer 1

4

what it's happening its that you are adding the event before the DOM exists, what you should do its wait for the response to actually render all the event and the interface or replace this:

  $("#photos a").click(function(event){
  });

for

  $(document).on("click","#photos a",function(){

});

that way the event its gonna exist always... Its my guess... I'm not sure if $("#photos a") actually get the element you want to attach the event, but you got the idea of how you can add event to DOM elements that still dont exist on the DOM.

enter image description here

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

4 Comments

It seems not to work... as soon as I click on a photo it redirects to the photo page instead of showing the lightbox
share with me your jsfiddle pls here
jsfiddle.net/23gzbqdh/4 its working there should be other problem in your code but this answer your question you can see the picture above in the answer I added debugger statement you can see it with google chrome inspector console
It works. Thank you very much. I'm just trying to figure out what is the problem with the rest of my code now.

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.