golam moktadir
golam moktadir

Reputation: 49

get current row number with column number

I have a text. Now I want if my text matched with any of column value then my method return that row and column number. My current code reads only column number, but I cant understand how can I get row number.

Sample table:

var index = $('tr td').filter(function() {
  return $(this).text() == 'Dhaka';
}).index();
console.log(index);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>Rion</td>
    <td>Bogra</td>
    <td>34</td>
  </tr>
  <tr>
    <td>Hasib</td>
    <td>Dhaka</td>
    <td>23</td>
  </tr>
</table>

If I give "Dhaka" as my given text then my code return column 1 but I need also row number as 2.

Upvotes: 3

Views: 849

Answers (1)

kukkuz
kukkuz

Reputation: 42352

You can use el.closest('tr').index() for the row number and as you already have, el.index() as the column number - see demo below:

var el = $('tr td').filter(function() {
    return $(this).text() == 'Dhaka';
  });
console.log(el.index(), el.closest('tr').index());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<table>
  <tr>
    <td>Rion</td>
    <td>Bogra</td>
    <td>34</td>
  </tr>
  <tr>
    <td>Hasib</td>
    <td>Dhaka</td>
    <td>23</td>
  </tr>
</table>

Upvotes: 1

Related Questions