Kaushil Rambhia
Kaushil Rambhia

Reputation: 195

Click event on td not firing

I'm generating the table in PHP and adding it to HTML using
I add PHP code to HTML in this way:

$('#pnametable').html(myVar);   

my PHP Code:

$str="<table id='notable'>";    
while($row=mysql_fetch_array($result,MYSQL_ASSOC)){       
   $str.="<tr id='gtpname'><td class='pnotd'>".$row['pname']."</td></tr>";       
}       
$str.="</table>";`   

Now my jQuery code goes like this:

$("#notable > tbody > tr > td").click(function() {       
  alert("wdwad");      
});

Please help me find solutions for it.

Upvotes: 0

Views: 2082

Answers (3)

G-Nugget
G-Nugget

Reputation: 8836

Your jquery selector is off. It is looking for a tbody, but your PHP code doesn't create a tbody.

You can either modify the PHP like this:

$str="<table id='notable'><tbody>";    
while($row=mysql_fetch_array($result,MYSQL_ASSOC)){       
   $str.="<tr id='gtpname'><td class='pnotd'>".$row['pname']."</td></tr>";       
}       
$str.="</tbody></table>";

Or change the jquery selector to this: $("#notable > tr > td")

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68400

It's not 100% clear how are you building your table, but it seems you're doing it dynamically. If this is the case, you need to use on method like this

$('#pnametable').on('click','#notable td', function(){
    alert("wdwad");
});

I'm assuming that this client code is building the table

$('#pnametable').html(myVar);   

Upvotes: 2

insomiac
insomiac

Reputation: 5664

The data which is generated is dynamic. Try using "on" method.

Upvotes: 3

Related Questions