Reputation: 626
This is my current code:
function tableClick(event){
$('#table').click(function(event){
alert(event.target.id);
});
}
<table id="table">
<tr>
<td id="td1" onclick="tableClick(this)">
1
</td>
</tr>
<tr>
<td id="td2" onclick="tableClick(this)">
2
</td>
</tr>
</table>
What I don't understand is why it pops up the alert window one more time than last. How can I fix this? Thanks in advance.
Upvotes: 0
Views: 60
Reputation: 44740
Because you are attaching click handler on every click -
You can do this -
function tableClick(this){
alert(this.id);
}
Upvotes: 1
Reputation: 78640
Each time you click, you call this function.
function tableClick(event){
$('#table').click(function(event){
alert(event.target.id);
});
}
This part:
$('#table').click(function(event){
attaches a click handler to the table. Every time you click you attach a new click handler to the table.
Upvotes: 4