Home Ask Login Register

Developers Planet

Your answer is one click away!

gene b. February 2016

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

I have a table with the structure

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

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?


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();

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


Leave an answer

Quote of the day: live life