Using querySelector() to obtain last td element

Doug Fir picture Doug Fir · Apr 1, 2015 · Viewed 16.1k times · Source

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?

Answer

Josh Crozier picture Josh Crozier · Apr 1, 2015

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;