Reputation: 195
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
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
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