0

So, I tried this:

$.ajax({  
    url: 'cod.php',  
    type: 'POST',  
    dataType: 'html',   
    success: function(res){  
    $("#tests").html(res);  
    }  

In my PHP, I have a foreach loop that brings me all data from a MySQL query.

<?php   
    foreach ($var as $row){  
    echo "<span class='name'>$row</span>";  
    }  
?>

The data provided by PHP are something like this:

<span class='click' id='name1'>Name1</span>  
<span class='click' id='name2'>Name2</span>  

The HTML shows correctly the PHP data, returned by Ajax. e.g.: Name1, Name2..

But, the click handler at the class: click doesn't works.

$('.click'),click(function(){    
    alert('testing');  
});  

Please, how do I solve this?

Thank's a lot! ;)

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
user3425150
  • 73
  • 1
  • 4

1 Answers1

1

Change , to . and use on method (Documentation) in jquery for dynamically loaded elements.

Try this:

$('.click').on('click', function() {

    alert('testing');
});
kpmDev
  • 1,330
  • 1
  • 10
  • 28
  • Thank's lot Vasu and stackoverflow.com . I'm reading the documentation now. Realy I wrote wrong "," instead of "." But, the point is about dinamically generate elements. I do not know enough of jquery's behavior, in this case. So, i realy have to lern some more! Thanks! ;) – user3425150 Mar 16 '14 at 22:24