Using querySelector() to obtain last td element
Asked Answered
L

1

19

I have a variable which is a node from the dom. I've managed to get all the way down to close to where I want to be:

myvar.querySelector('.tblItinPriceSummary tr')

Gives me this:

<tr>
    <td>Subtotal</td>
    <td align="right">$189.00</td>
</tr>

What I want is the textContent of the second td $189.

Is there anything I can add inside of querySelector so that I can append it with .textContent to get this piece of data?

Langdon answered 1/4, 2015 at 19:12 Comment(1)
G
28

You could either use :last-child or :last-of-type to access the last td element within the parent.

document.querySelector('.tblItinPriceSummary tr td:last-child').textContent;
Greatuncle answered 1/4, 2015 at 19:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.