Marko
Marko

Reputation:

How can I get the value from the last td in the tr tag in jQuery?

I have a table when I click on a table row I can fetch the row with this but how can I get the value from the last td in the tr tag?

$(this); // equals my hovered tr row 

I thought of something like this:

$(this:last-child).text(); 

Or should it be like this:

$(this + ":last-child").text(); or with .html(); 

Upvotes: 0

Views: 10083

Answers (3)

Mr Singh
Mr Singh

Reputation: 4220

console.log($( "table#hello td:nth-last-child(1)" ).text())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="masteries" id='hello'>
        <tr class="p0">
            <td>1</td>
            <td>2</td>
            <td>3,</td
        </tr>
        <tr class="p4">
            <td>1</td>
            <td>2</td>
            <td>3,</td
        </tr>
        <tr class="p8">
            <td>1</td>
            <td>2</td>
            <td>3,</td
        </tr>
    <table>

Upvotes: 0

karim79
karim79

Reputation: 342635

$('#' + this.id + ' :last-child').text();

is the same as:

$('#idOfMyTr :last-child').text();

Upvotes: 0

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827306

You can try with:

$(this).find(':last-child').text();

Or :

$(':last-child', this).text();

Upvotes: 7

Related Questions