Skip to content

Instantly share code, notes, and snippets.

@ephsmith
Last active July 2, 2018 11:19
Show Gist options
  • Save ephsmith/82785f18d46a4c065122cdf1ce2f9071 to your computer and use it in GitHub Desktop.
Save ephsmith/82785f18d46a4c065122cdf1ce2f9071 to your computer and use it in GitHub Desktop.
Add click events to all rows in an HTML table
/* See: http://jsfiddle.net/oh16h7cj/ */
function addRowHandlers() {
var table = document.getElementById("tableId");
var rows = table.getElementsByTagName("tr");
for (i = 0; i < rows.length; i++) {
var currentRow = table.rows[i];
var createClickHandler =
function(row)
{
return function() {
var cell = row.getElementsByTagName("td")[0];
var id = cell.innerHTML;
alert("id:" + id);
};
};
currentRow.onclick = createClickHandler(currentRow);
}
}
window.onload = addRowHandlers();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment