Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find row and col number of a cell in table

Tags:

javascript

dom

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);
}
like image 669
Mark Tomlin Avatar asked May 08 '12 07:05

Mark Tomlin


People also ask

How do I find the row and column number of a cell in Excel?

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.

How do I find the row number in a cell?

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.

How do you identify the row and the column of a cell?

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.

How do I get the column number in a cell?

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.


3 Answers

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

like image 116
Sampson Avatar answered Oct 18 '22 14:10

Sampson


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>
like image 27
Jazzzzzz Avatar answered Oct 18 '22 14:10

Jazzzzzz


window.onload = function () {
 document.getElementsByTagName('table')[0].addEventListener('click', function(e) {
  alert(e.target);
 }, false);
}
like image 1
Musa Avatar answered Oct 18 '22 15:10

Musa