adding onclick event to dynamically generated array of element jquery

后端 未结 3 820
北恋
北恋 2021-01-28 11:30

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 usi

相关标签:
3条回答
  • 2021-01-28 12:03

    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"

    0 讨论(0)
  • 2021-01-28 12:06

    Please move the sliderimage() function outside of Ajax call

     $.ajax({  
     });
    
     function sliderimage(image){     
       alert("sdf");
       alert(image);
     }
    
    0 讨论(0)
  • 2021-01-28 12:14

    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

    0 讨论(0)
提交回复
热议问题