If JavaScript is being used to build the HTML table, you can attach the event handlers at runtime without needing any IDs.
A good example of this would be when you're building an HTML structure by looping through an array of results via JavaScript. In such a case, you can use closures to maintain references to the result data that corresponds to particular HTML elements, and use that data when event handlers are invoked.
In the example below, the event handler that is attached to each button is able to directly call up the value
property of a corresponding object in an array; we didn't need to add anything to the button element (neither an ID nor a data- attribute) to maintain that reference.
var dataResults = [{title:"One",value:1}, {title:"Two",value:2}, {title:"Three",value:3}, {title:"Four",value:4}];
var output = document.getElementById("output");
for (var i = 0, len = dataResults.length; i < len; i++) { // 1. Loop through results
var btn = document.createElement("button"); // 2. Create your HTML element(s)
btn.value = dataResults[i].title; // 3. Set HTML based on result values
btn.innerHTML = dataResults[i].title;
(function(result) { // 4. Use a closure to capture the current result item
btn.addEventListener("click",function() {
alert("You clicked button " + result.value);
}); // 5. Add an event handler that references the result item.
})(dataResults[i]); // (Pass the current result item into the function expression as a parameter)
output.appendChild(btn); // 6. Add your HTML to the page.
}
<div id="output"></div>
Of course, none of that is relevant if JavaScript isn't responsible for building the HTML in your application!