Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using querySelector() to obtain last td element

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?

like image 639
Doug Fir Avatar asked Apr 01 '15 19:04

Doug Fir


People also ask

How do I select the last element in querySelectorAll?

To get the last element with specific class name: Use the querySelectorAll() method to get a NodeList containing the elements with the specific class. Convert the NodeList to an array. Use the pop() method to get the last element of the array.

What is the use of querySelector () method?

The querySelector() method in HTML is used to return the first element that matches a specified CSS selector(s) in the document. Note: The querySelector() method only returns the first element that matches the specified selectors. To return all the matches, use the querySelectorAll() method.

How do I get attributes from querySelector?

How it works: First, select the link element with the id js using the querySelector() method. Second, get the target attribute of the link by calling the getAttribute() of the selected link element. Third, show the value of the target on the Console window.


1 Answers

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;
like image 59
Josh Crozier Avatar answered Oct 07 '22 01:10

Josh Crozier