-4
<table>
<tr>
<td id="1">Adi</td>
<td id="2">Aman</td>
</tr>
</table>

In the above code, I want to know the position of Aman using its id

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 1
    Does this answer your question? [Table row and column number in jQuery](https://stackoverflow.com/questions/788225/table-row-and-column-number-in-jquery) – Skully Mar 12 '21 at 07:59
  • Could you correct a couple of things in your question? I think there is a typo in your question title ("I'd") and some missing code in your question body. It's also not clear what `rs` in the title refers to. – Cat Mar 12 '21 at 08:00

2 Answers2

0

You can try something like this:

html:

<table id="myTable">
   <tr>
      <td id="1">Adi</td>
      <td id="2">Aman</td>
   </tr>
</table>

js:

function getIdFromTable(searchValue)
{
  var t = document.getElementById("myTable");
  var trs = t.getElementsByTagName("tr");

  var tds = null;

  for (var i=0; i<trs.length; i++)
  {
      tds = trs[i].getElementsByTagName("td");
      for (var n=0; n<tds.length;n++)
      {
        if (tds[n].innerText === searchValue) {
          return tds[n].id;
        }
      }
  }
}

getIdFromTable('Aman'); // will return 2
JakobHeltzig
  • 163
  • 1
  • 1
  • 7
0

Easiest way to find position by id would be using prevAll().length. Something like this:

function findPositionById(id){
  return $('#mytable').find('#'+id).prevAll().length
}

console.log('Adi Position', findPositionById(1));
console.log('Aman Position', findPositionById(2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<table id="mytable">
<tr>
<td id="1">Adi</td>
<td id="2">Aman</td>
</tr>
</table>
dmikam
  • 992
  • 1
  • 18
  • 27