jQuery - Getting the text value of a table cell in the same row as a clicked element

JqueryJquery Selectors

Jquery Problem Overview


I click a link in a table cell. I need to get the value of a specific cell within this same table row.

<tr>
    <td class="one">this</td>
    <td class="two">that</td>
    <td class="three">here</td>
    <td class="four"><a href="#">there</a></td>
</tr>
<tr>
    <td class="one">qwqw</td>
    <td class="two">dfgh</td>
    <td class="three">ui</td>
<td class="four"><a href="#">there</a></td>
</tr>

I have a click handler attached to the link in the fourth cell. That click handler calls a function that opens a modal window. When a form in the modal is submitted I want to also pass the value of td class="two" from the row in which the link was clicked to this modal.

Here is the function that sends the modal (the problem area is getting the correct value for var Something):

var Send = function() {
    var Name = $( '#name' ).val();
    var Something = $(this).closest('td').siblings('.two').text(); // version 1. doesn't work
    var Something = $(this).closest('tr').siblings('td.two').text(); // version 2 also doesn't work
    var Something = $(this).attr('class'); // version 3. just a test but also doesn't work
    $.ajax( {
        async: false,
        data: { name: Name, somedata: Something },
        type: 'POST',
        url: the url
    });        
};

The problem is that I cannot get the correct value for Something. It should be the value of td class=two in the same row as the clicked element.

The way this all comes together is. Click the target link, that calls a method called Send_Click(). Send_Click does some validations and then calls Send() but the value for Something is never populated. Is this because this is not what I'm thinking it is? Hjelp!

Jquery Solutions


Solution 1 - Jquery

You want .children() instead (documentation here):

$(this).closest('tr').children('td.two').text();

Solution 2 - Jquery

Nick has the right answer, but I wanted to add you could also get the cell data without needing the class name

var Something = $(this).closest('tr').find('td:eq(1)').text();

:eq(#) has a zero based index (link).

Solution 3 - Jquery

it should work fine:

var Something = $(this).children("td:nth-child(n)").text();

Solution 4 - Jquery

so you can use parent() to reach to the parent tr and then use find to gather the td with class two

var Something = $(this).parent().find(".two").html();

or

var Something = $(this).parent().parent().find(".two").html();

use as much as parent() what ever the depth of the clicked object according to the tr row

hope this works...

Solution 5 - Jquery

This will also work

$(this).parent().parent().find('td').text()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionrg88View Question on Stackoverflow
Solution 1 - JqueryNick CraverView Answer on Stackoverflow
Solution 2 - JqueryMottieView Answer on Stackoverflow
Solution 3 - JqueryhhhqsunView Answer on Stackoverflow
Solution 4 - JqueryYasin ErgulView Answer on Stackoverflow
Solution 5 - JqueryAnna.PView Answer on Stackoverflow