I have a simple piece of code that in my mind should work
HTML
<table cellpadding="0" cellspacing="0" border="0">
<tbody><tr>
<th scope="row">Quantity</th>
<th id="js-QuantityID-1" scope="col">1</th>
<th id="js-QuantityID-2" scope="col">2</th>
<th id="js-QuantityID-3" scope="col">3</th>
<th id="js-QuantityID-4" scope="col">4</th>
<th id="js-QuantityID-5" scope="col">5</th>
<th id="js-QuantityID-6" scope="col">10</th>
<th id="js-QuantityID-7" scope="col">15</th>
<th id="js-QuantityID-8" scope="col">20</th>
<th id="js-QuantityID-9" scope="col">30</th>
<th id="js-QuantityID-10" scope="col">40</th>
<th id="js-QuantityID-11" scope="col">100</th>
</tr>
<tr>
<th scope="row">Price (inc. VAT)</th>
<td id="js-PriceID-1">£45.60</td>
<td id="js-PriceID-2">£76.80</td>
<td id="js-PriceID-3">£97.20</td>
<td id="js-PriceID-4">£128.40</td>
<td id="js-PriceID-5">£172.80</td>
<td id="js-PriceID-6">£307.20</td>
<td id="js-PriceID-7">£402.00</td>
<td id="js-PriceID-8">£432.00</td>
<td id="js-PriceID-9">£630.00</td>
<td id="js-PriceID-10">£840.00</td>
<td id="js-PriceID-11">£2100.00</td>
</tr>
</tbody>
</table>
Javascript
function getTableContents() {
var productArray = [];
for (var x = 0; x <= 12; x++) {
productArray.push({ Price: $('#js-PriceID-' + x).text(), Qty: $('#js-QuantityID-' + x).text() });
}
console.log("Array: " + productArray);
}
But at the end of the execution of this code, I end up with an array with the two properties 'undefined'. If I manually enter the selector ID it works fine, it seems to be with the for loop and getting the values at runtime.
Why is this and is there a workaround?