In JavaScript, how can I refer to the button that called the function?
I have an HTML table. One of the cells in each row of the table contains a button. When the button is clicked, the row that contains the button should be removed from the table.
The button calls the removeItem function when it is clicked.
<input type="button" value="Remove" onclick="removeItem()" />
.
function removeItem(sender) {开发者_如何转开发
var tableTBody = document.getElementById('my_tbody');
tableTBody.removeChild(sender.parentNode.parentNode);
}
In the function I used sender
to stand for the reference to the button. The button's parent is the cell ( element) that contains it, and the cell's parent is the row that contains it.
How can I refer inside the function to the button that called the function?
<input type="button" value="Remove" onclick="removeItem(this)" />
精彩评论