2

I have a table. Each row has an ID.

Can I get the index of any row by using only its ID (no click) and without using a loop?

My HTML looks like this:

<table id="tabId">
<tr id="a">
    <td>ss</td>
    <td>ss</td>
</tr>
<tr id="b">
    <td>kk</td>
    <td>kk</td>
</tr> 

I tried this:

var row_number = document.getElementById('a').rowIndex;

but it's not working; I get an error message saying:

[ts] Property 'rowIndex' does not exist on type 'HTMLElement'.

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
user2243952
  • 277
  • 3
  • 6
  • 12
  • 4
    It's a [property and not a method](https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement/rowIndex) -> `document.getElementById(a).rowIndex` – Andreas Nov 10 '17 at 07:25
  • it's not a duplicate. already read that post. – user2243952 Nov 10 '17 at 07:32
  • @user2243952 here is the [answer](https://stackoverflow.com/a/37613166/3551786). And there `this` is same what you are getting by id. – Durga Nov 10 '17 at 07:33
  • no, that solution need to click on the row. I need the index using only the id – user2243952 Nov 10 '17 at 07:42
  • The click is only to get the row, so yes you would have to adjust it a little bit... – Andreas Nov 10 '17 at 07:56
  • @Andreas OP is looking answer for typescript, now I am also confused, my answer does works for me, but why not for him. – Durga Nov 10 '17 at 07:59
  • 2
    [The property 'value' does not exist on value of type 'HTMLElement'](https://stackoverflow.com/questions/12989741/the-property-value-does-not-exist-on-value-of-type-htmlelement) -> Cast as `HTMLTableRowElement` – Andreas Nov 10 '17 at 08:12
  • @ Alexandru-IonutMihai: The OP has clarified in their comments that they're using TypeScript, for which the solutions given [there](https://stackoverflow.com/questions/37573622/how-do-i-get-current-rowindex-of-table-using-javascript) apparently don't work. – Ilmari Karonen Nov 10 '17 at 16:44

6 Answers6

2

You forgot to add '' to the getElementById() function and you wrote rowIndex not as a property and wrong.

Here is a working example:

var row_number = document.getElementById('a').rowIndex;
alert(row_number);
<table id="tabId">
<tr id="a">
    <td>ss</td>
    <td>ss</td>
</tr>
<tr id="b">
    <td>kk</td>
    <td>kk</td>
</tr> 
Dario
  • 618
  • 1
  • 11
  • 28
2
var el = (document.getElementById('b')) as HTMLTableRowElement;
console.log(el.rowIndex);
klugjo
  • 19,422
  • 8
  • 57
  • 75
0
var rows = Array.prototype.slice.call(document.getElementById('tabId').rows);
var row = document.getElementById('b');

console.log(rows.indexOf(row));

Here is an Example

Alex
  • 4,885
  • 3
  • 19
  • 39
klugjo
  • 19,422
  • 8
  • 57
  • 75
0

special thx to @durga for his solution. I fixed it by explicitly typing variable as any.

var el : any = (document.getElementById('a')) as HTMLTableElement;
var index = el.rowIndex;
console.log(index);  
user2243952
  • 277
  • 3
  • 6
  • 12
-1

you can get using on click on particulate tr to get its index

or change you Rowindex to rowIndex and add '' in id of div 'a' in script

function myFunction(x) {
  console.log("Row index is: " + x.rowIndex);
}
<table id="tabId">
  <tr id="a" onclick="myFunction(this)">
    <td>ss</td>
    <td>ss</td>
  </tr>
  <tr id="b" onclick="myFunction(this)">
    <td>kk</td>
    <td>kk</td>
  </tr>
Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39
-2
document.getElementsByTagName("tr")[index].id;


<table id="tableId">
    <tr id="a">
        <td>a</td>
    </tr>
    <tr id="b">
        <td>b</td>
    </tr>
</table>

var table = document.getElementById("tableId");
var row = table.rows[index];
console.log(row.id);
bklups
  • 300
  • 1
  • 13