Martyn Ball Martyn Ball - 9 months ago 43
HTML Question

.each() function on affect current object

having some issues with my code below, first here is the HTML:

<table class="finance-table">
<th>Total Payable</th>
<th>Fixed Rate</th>
<th>Representative APR</th>

<th>Monthly Pmt</th>

<tr class="hp">
<td id="td_finance_deposit">£11700.00</td>
<td id="td_finance_balance">£105300.00</td>
<td id="td_finance_fees">£298.00</td>
<td id="td_finance_total_inc_deposit">£146255.50</td>
<td id="td_finance_term">60 mths</td>
<td id="td_finance_rate">5.50%</td>
<td id="td_finance_apr">10.1%</td>

<td id="td_finance_monthly_payments">£2242.59 p/m* x 60 mths</td>


There is about 10 of these tables [within the same document], all with the same id's and class's. I'm using an each loop to execute some code against each table found, however it only seems to be working on the first table and disregards the others.

Below is the jQuery, like I said works find on the first table, but ignores the rest!

<!-- Remove First and Final Payment from Showroom Finance Examples -->
$(".finance-table").each(function(key, value) {
// Display loading
var html = $(this);

// Remove the First Payment and Final Payment Column
$(this).find("#td_finance_first_payment, #td_finance_final_payment").remove();
$(this).find("th:contains('1st Pmt')").remove(); $(this).find("th:contains('Final Pmt')").remove();

// Get the Term and update the monthly payment
var term = $(this).find("#td_finance_term").html(); // .replace(/\D/g,'')
var payments = ($(this).find("#td_finance_monthly_payments").html()).split('x')[0];
($(this).find("#td_finance_monthly_payments")).html(payments + " x " + term);

Please note, I can't change the HTML at all

Answer Source

You should first give a unique ID to each <td>, perhaps with your DB identifier for that record. You don't need it now but this will allow you to do other thing later if you need it.

Then change all the <td> ids to classes:

<td class="td_finance_fees">£298.00</td>

Finally change all your javascript accordingly to use class instead of IDs:

$(this).find(".td_finance_first_payment, .td_finance_final_payment").remove();