0

I want to get id of an appended element by jquery, but i cant, this is my code: append elements:

$(document).ready(function(){
 $("#addSlide").click(function(){
 var slideId="slide"+n;
 var  nslide="<td><img class='projImgs'id='"+slideId+"'onclick='openCustomRoxy()' src='' style='width:75px;height:55px'/> <input id='sldAddr' name='"+slideId+"'   type='hidden' value='' /> </td>";
 $("#slideTr").append(nslide);  
});

and The code that get atrribute 'id' :

$(".projImgs").click(function(){
var id= $(this).attr('id');
console.log(id);
var source= "http://localhost/exx/admin/fileman/index.html?integration=custom&type=files&img="+id;
console.log(source);
$("#frame").attr('src',source);
});
Hamid
  • 21
  • 5

2 Answers2

2

delegate it with on for dynamically added element to its closest parent which I assume is $("#slideTr")

 $("#slideTr").on("click",".projImgs",function(){
     var id= $(this).attr('id');
         ....
  });
bipen
  • 36,319
  • 9
  • 49
  • 62
1

Use Event delegation for added dymanically DOMs . Use immediate parent selector for traverse easily and quickly. If you didn't know the immedaite parent using $(document) instead

$('#slideTr').on('click', '.projImgs', function(){
     // your code
});
Sudharsan S
  • 15,336
  • 3
  • 31
  • 49