Not using jQuery, what is the most efficient way of getting the Row and Col (X, Y) of a onClick even within a table?
I would think that assigning a click listener to just the table, and let it bubble to the top would work well, but really it just gives me the HTMLTableElement. What I want to get from it is the Col number, and Row number from this one listener. Is that possible?
window.onload = function () {
document.getElementsByTagName('table')[0].addEventListener('click', function() {
alert(this.tagName);
}, false);
}
You could bind to the table, but that would leave open the possibility that you might click within the spacing between cells (which doesn’t have a row or cell index). I have decided in the example below that I would bind to the cells themselves, and thus ensure I would always have a row and cell index.
Demo: http://jsbin.com/isedel/2/edit#javascript,html
I suppose you could safely bind to the table itself too, and perform a check against the source element to see if it was a cell or not:
Demo: http://jsbin.com/isedel/5/edit