Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get current rowindex of a table using Javascript?

Can I get current row index of a table in Javascript and can we remove the row of table with current Index that we got?

like image 558
nara son Avatar asked Dec 14 '22 06:12

nara son


2 Answers

The rowIndex property returns the position of a row in table

function myFunction(x) {
  console.log("Row index is: " + x.rowIndex);
}
<table>
  <tr onclick="myFunction(this)">
    <td>Click to show rowIndex</td>
  </tr>
  <tr onclick="myFunction(this)">
    <td>Click to show rowIndex</td>
  </tr>
  <tr onclick="myFunction(this)">
    <td>Click to show rowIndex</td>
  </tr>
  <tr onclick="myFunction(this)">
    <td>Click to show rowIndex</td>
  </tr>
</table>
like image 168
Mahendra Kulkarni Avatar answered Dec 28 '22 12:12

Mahendra Kulkarni


If you are using JQuery, use method .index()

var index = $('table tr').index(tr);

If no JQuery used, you can loop through all the TR element to find the matched TR.

var index = -1;
var rows = document.getElementById("yourTable").rows;
for (var i=0;i<rows.length; i++){
    if ( rows[i] == YOUR_TR ){
        index = i;
        break;
    }
}
like image 30
Felton Fei Avatar answered Dec 28 '22 12:12

Felton Fei