Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find row and column of clicked table cell in JavaScript without jQuery [duplicate]

I'm trying to create an onclick function that returns the row and the column of the clicked cell from a table in JavaScript without using jQuery. What I have so far will return the column, but I can't figure out how to make it return the row.

  **cellToAppend.addEventListener("click", clicked);**

  **function clicked() {
    alert("clicked cell at: " + (this).cellIndex + ", ");
  }**
like image 647
ThomYorkkke Avatar asked Feb 06 '15 01:02

ThomYorkkke


1 Answers

Use parentNode to get the row.

function clicked() {
    alert("clicked cell at: " + this.cellIndex + ", " + this.parentNode.rowIndex);
}
like image 148
Barmar Avatar answered Nov 02 '22 14:11

Barmar