I have an array of values "purchasedDiscount" that I parse from HTML. Outside of the loop it holds a value. Inside the loop I get output when using a 0 in the index. When I try to get the values using the counter "i" from the for loop I get "undefined". I have no idea why this is happening. I am using Prototype JS, but I feel this is a JS issue directly with my syntax (could be wrong).
var purchasedDiscount = $$('.cm-purchased-discount > span.price');
console.log("Purchased Discount: " + purchasedDiscount[0].innerHTML);
for (i = 0; i < cmItems.length; i++) {
console.log("i: " + i);
console.log("length of loop: " + cmItems.length);
console.log(parseFloat(stripPriceFormatting(purchasedPrice[i].innerHTML)));
console.log("Purchased Discount 0: " + purchasedDiscount[0].innerHTML);
console.log("Purchased Discount i where i is 0: " + purchasedDiscount[i].innerHTML);
}
Here is my Console output
(index):1280 Purchased Discount: £0.00
(index):1287 i: 0
(index):1288 length of loop: 2
(index):1289 679
(index):1290 Purchased Discount 0: £0.00
(index):1291 Uncaught TypeError: Cannot read property 'innerHTML' of undefined
stripPriceFormatting Delcation
function stripPriceFormatting(price) {
var priceFormats = ["$", "£"];
var strippedFormatting = price;
for (i=0; i < priceFormats.length; i++) {
strippedFormatting = strippedFormatting.replace(priceFormats[i], "");
}
console.log("Stripped Formatting: " + strippedFormatting);
return strippedFormatting.replace(",", "");
}