-1

I have 7 images in my index HTML, and then I use a jQuery function, when I click the images the function gives me images' ids.

The function looks like:

$(".imgss").click(function() {

  alert($(this).attr("id"));

});

.imgss is a class that iIgive, but I select another city in my select box and I send variable to PHP and AJAX gives me result of other new images.

When the new image is downloaded, I try to click the images but jQuery doesn't work.

New images also have .imgss class.

This is my php code to create images :

print_r ("<div class='responsive' id='salonlarimi_goster'> 
<img src='$salonlarimi_ver[f1]' alt='salon_foto_5' width='250' height='250' 
id='$salonlarimi_ver[klavuz]' class='gelenimg'> 
<div class='desc'>$salonlarimi_ver[salon_isim]</div> </div>"); 
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • Try using `$(".imgss").on('click', function() { ... });` as your event handler – KyleS Jun 27 '17 at 12:47
  • Can you share the code for refreshing the images after making a selection on city select box. It could be an issue with your image ids not getting registered to DOm after the ajax refresh. – Arun Chidanandan Jun 27 '17 at 12:50
  • hi, i tagged php code because i used php to take new images in another php file, i thought maybe it is relevant to this situation. and i use this code to new images print_r ("
    salon_foto_5
    $salonlarimi_ver[salon_isim]
    "); }
    – Ömürcan Özcan Jun 27 '17 at 13:01
  • it could have relevance, we don't know that. You have that code and you should also include the HTML for all this, but if you feel you don't have to, then see the answers below and accept the best one you think solved the question then @Ömürcan and please don't drop code in comments, edit your question instead https://stackoverflow.com/posts/44780666/edit – Funk Forty Niner Jun 27 '17 at 13:03
  • sorry for some troubles, i am new in computer world, i try to learn the rules, thanks for your patience Fred – Ömürcan Özcan Jun 27 '17 at 13:16

3 Answers3

1

I think you are working with dynamic html content, so instead of:

$(".imgss").click(function() {   // This works for static html
  alert($(this).attr("id"));
});

try

$(document).on('click', '.imgss', function() {   // This works for static as well as dynamic html
    alert($(this).attr("id"));
});

Explanation: The html which are loaded by ajax() are dynamic. To deal with that you have to use appropriate event listener.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
1

try this code. because when you download new image than jquery dont know about it.

$(document).on("click",".imgss",function() {
  alert($(this).attr("id"));
});
Nirav Joshi
  • 2,924
  • 1
  • 23
  • 45
0
$('.imgss').on('click', function() { 
  alert();
});
Reena Mori
  • 647
  • 6
  • 15