gene b. February 2016

In jQuery get neighboring cell Data (Same Row) for each row link

I have a table with the structure

<table>
  <tr>
    <td class="IDCol">
        Some data...
    </td>
    <td class="LinkCol">
        <a href="..">Link</a>
    </td>
    <!-- etc. -->
  </tr>
</table>

A jQuery iterator needs to go through all the links and capture the neighboring data in the corresponding Column IDCol to the left:

$('#myTable a[id^="linkIndex"]').off("click.linkIndex").on("click.linkIndex", function() {
   // here, need to get the corresponding "IDCol" contents for this link row
   var idContents = ...;
});

Is there an easy way to do this?

Answers


Azim February 2016

Your link td has class LinkCol. Use this to iterate through each link and get the text of previous td using prev() function like following.

$('.LinkCol').each(function() {
    var idcol = $(this).prev('.IDCol').text();
    console.log(idcol);
});


Roamer-1888 February 2016

Assuming the a[id^="linkIndex"] selector to be correct, try :

$('#myTable a[id^="linkIndex"]').off("click.linkIndex").on("click.linkIndex", function() {
   var idContents = $(this).closest("td").prev("td.IDCol").text(); // or maybe .html()
});

Post Status

Asked in February 2016
Viewed 3,014 times
Voted 9
Answered 2 times

Search




Leave an answer