Carol Kariuki Carol Kariuki - 4 months ago 23
jQuery Question

Getting elements inside other Elements Javascript

I know this question has been asked before, but not in this way...
I have this HTML.

<table class="fr ralign cartSummaryTable">
<tr>
<td width="320px">
<div class="tr">
Subtotal (5 items):
</div>
</td>
<td>
<div class="nowrap tr normal">$684.23
</div>
</td>
</tr>
<tr>
<td width="320px">
<div class="tr">
<span> Shipping to <a id="guestZipLink" href="javascript:;">19802-4358 </a>
</span></div>
</td>
<td>
<div class="tr nowrap normal">$4.49
</div>
</td>


I want to get the second price $4.49.

My code is:

price= $('#cartSummaryTable tr:eq(1) td:eq(1) td:eq(1) .tr:eq(1)').textContent;
console.log("My price is" + price);


I however get undefined as my result.
What I'm I doing wrong, and what's the correct way to do it?

Answer

To get the text inside that cell (i.e. $4.49):

var price = $("table.cartSummaryTable tr:eq(1) td:eq(1) div").text();

To get the price as a number, so you could do numeric calculations on it:

var price_as_float = parseFloat($("table.cartSummaryTable tr:eq(1) td:eq(1) div").text().substring(1));