0

I have a dynamically generated thumbnail images on the left side of my popup and on click of the image i want that image to be displayed on the right hand side.

I am using the below code:

    $.ajax({  
                    type: "POST",
                    url:  "/buildyourholiday/placeimages",
                    data: "srcid="+cityid,
                    success: function(data){
                        var objsrc = jQuery.parseJSON(data);
                        var src="";
                        $(".photoright").append().empty();
                        $.each(objsrc, function(index, obsrc){
                            src += '<ul>'+
                                '<li><a href="#"><img id="citythumb" onclick="sliderimage(this)" src="/images/city/thumbs/'+obsrc.img_filename+'" width="100" height="100"></a></li>'+
                                '</ul>';
                        });
                        $(".photoright").append(src);
                        function sliderimage(image)
{
    alert("sdf");
    alert(image);
}
                    }
                });

When i firebug it i get sliderimage is not a function. When i use the onclick event after append i get only the first image, but there are multiple images. I want the onclick to work on the click of any image. How to go about it?

Thanks,

1
  • please make alert using parameter of sliderimage function and tell whether it works for all images Commented Oct 3, 2012 at 8:06

3 Answers 3

1

id="citythumb" must be unique, use class, like class="citythumb"

$(".photoright").on('click', 'img', function(e){
   e.preventDefault(); // for link
   alert(this);
});

when you change id attribute, you could also replace img with .citythumb

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

Comments

0

Try event delegation approach here where in the events are associated to the elements ancestor ..

By doing so because of bubbling effect the click event will be associated to the image element in such cases...

$(".photoright").on('click', 'img', function(){
   alert(this);
});

Make sure the id in your document is unique.

So instead of id="citythumb" declare class="citythumb"

Comments

0

Please move the sliderimage() function outside of Ajax call

 $.ajax({  
 });

 function sliderimage(image){     
   alert("sdf");
   alert(image);
 }

Comments

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.