Last active
July 2, 2018 11:19
-
-
Save ephsmith/82785f18d46a4c065122cdf1ce2f9071 to your computer and use it in GitHub Desktop.
Add click events to all rows in an HTML table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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