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);
}
Just click the column header. The status bar, in the lower-right corner of your Excel window, will tell you the row count. Do the same thing to count columns, but this time click the row selector at the left end of the row. If you select an entire row or column, Excel counts just the cells that contain data.
In the first cell of the range that you want to number, type =ROW(A1). The ROW function returns the number of the row that you reference. For example, =ROW(A1) returns the number 1.
Understanding cells A cell is the intersection of a row and a column—in other words, where a row and column meet. Columns are identified by letters (A, B, C), while rows are identified by numbers (1, 2, 3). Each cell has its own name—or cell address—based on its column and row.
The COLUMN function returns the column number of the given cell reference. For example, the formula =COLUMN(D10) returns 4, because column D is the fourth column.
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.
var tbl = document.getElementsByTagName("table")[0];
var cls = tbl.getElementsByTagName("td");
function alertRowCell(e){
var cell = e.target || window.event.srcElement;
alert( cell.cellIndex + ' : ' + cell.parentNode.rowIndex );
}
for ( var i = 0; i < cls.length; i++ ) {
if ( cls[i].addEventListener ) {
cls[i].addEventListener("click", alertRowCell, false);
} else if ( cls[i].attachEvent ) {
cls[i].attachEvent("onclick", alertRowCell);
}
}
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:
var tbl = document.getElementsByTagName("table")[0];
function alertRowCell (e) {
var cell = e.target || window.event.srcElement;
if ( cell.cellIndex >= 0 )
alert( cell.cellIndex + ' : ' + cell.parentNode.rowIndex );
}
if ( tbl.addEventListener ) {
tbl.addEventListener("click", alertRowCell, false);
} else if ( tbl.attachEvent ) {
tbl.attachEvent("onclick", alertRowCell);
}
Demo: http://jsbin.com/isedel/5/edit
Here is the most simple way to do that.
window.onload = function () {
document.querySelector('#myTable').onclick = function(ev) {
var rowIndex = ev.target.parentElement.rowIndex;
var cellIndex = ev.target.cellIndex;
alert('Row = ' + rowIndex + ', Column = ' + cellIndex);
}
}
<table border="1" id="myTable">
<tr>
<td>first row first column</td>
<td>first row second column</td>
</tr>
<tr>
<td>second row first column</td>
<td>second row second column</td>
</tr>
<tr>
<td>third row first column</td>
<td>third row second column</td>
</tr>
</table>
window.onload = function () {
document.getElementsByTagName('table')[0].addEventListener('click', function(e) {
alert(e.target);
}, false);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With