0

I have a table added dynamically to my page.

$("#myTable").on("click", "tr .eliminar.tooltip", function() {
  var myCol = $("#myTable").index(); //ok
  var $tr = $("#myTable").closest('tr'); // ok
  var myRow = $tr.index(); // wrong value
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="uk-table" id="myTable">

  <tbody>
    <tr data-myData="001">
      <td>
        <a class="editar tooltip">
          <img src="Editar.png">
        </a>
        <a class="eliminar tooltip">
          <img src="Eliminar.png">
        </a>
      </td>
    </tr>
    <tr data-myData="002">
      <td>
        <a class="editar tooltip">
          <img src="Editar.png">
        </a>
        <a class="eliminar tooltip">
          <img src="Eliminar.png">
        </a>
      </td>
    </tr>
  </tbody>
</table>

How can I get the info? when clicking, I want to get the row number to get the data.attr("myData").

Thats the jquery code:

1
  • "I want to get the row number to get the data" - so you don't need the row number at all? Just the data? meta.stackexchange.com/a/66378 Commented Nov 7, 2016 at 10:34

3 Answers 3

5

You need to use this i.e. current element context to get the tr element and .index(element)

$("#myTable").on("click", "tr .eliminar.tooltip", function() {
    var $tr = $(this).closest('tr');
    var myRow = $("#myTable tr").index($tr);
    var myData = $tr.attr('data-myData')
    console.log(myRow, myData);
});

jQuery(function($) {
  $("#myTable").on("click", "tr .eliminar.tooltip", function() {
    var $tr = $(this).closest('tr');
    var myRow = $("#myTable tr").index($tr);
    var myData = $tr.attr('data-myData')
    console.log(myRow, myData);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="uk-table" id="myTable">

  <tbody>
    <tr data-myData="001">
      <td>
        <a class="editar tooltip">
          <img src="Editar.png">
        </a>
        <a class="eliminar tooltip">
          <img src="Eliminar.png">
        </a>
      </td>
    </tr>
    <tr data-myData="002">
      <td>
        <a class="editar tooltip">
          <img src="Editar.png">
        </a>
        <a class="eliminar tooltip">
          <img src="Eliminar.png">
        </a>
      </td>
    </tr>
  </tbody>
</table>

Sign up to request clarification or add additional context in comments.

Comments

4

Try this:

Get COLUMN INDEX on click:

$(this).closest("td").index();

Get ROW INDEX on click:

$(this).closest("tr").index();

Comments

1

You should be able to target each tr using the .closest() function based on the td clicked. And using the .attr() function to get the value of "data-myData"

var tr = $(this).closest('tr');
var data = tr.attr("data-myData");

I have made a fiddle as an example.

https://jsfiddle.net/gpo0htp5/

1 Comment

To make it clear where to put this code, add the click handler. Also, you can use .data("myData").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.