I have a <td>
that when I click on it, it shows me a alert with this message: clicked
My problem is when I append a new <td>
, then when I click on it, I don't see any alert. Why? And how can I set event for appended elements?
Here is a fiddle that explains what I said.
HTML:
<table> <tr class = 'test'> <td> old </td> </tr> </table>
<br><br>
<div class ='addnewrow'>append new row</div>
CSS:
.addnewrow{
border:2px solid;
padding: 4px;
text-align: center;
cursor: pointer;
}
td{
cursor: pointer;
}
JS:
$(".addnewrow").click(function(){
$("table").append('<tr><td>new</td></tr>');
});
$("td").click(function(){
alert('clicked');
});