I'm currently trying to calculate the percentage off -- or difference in percent between two prices on a web store. I also need to be able to loop through all items on the page that have the same classname applied.
Inside the class name div there is some text contained in an empty that needs to be trimmed in order to calculate the percentage. So, I have to remove the text part of the string, keep the numbers and do the math.
The HTML output I'm working with looks like this:
<div class="price-wrap">
<div class="price regular-price">
<span>Retail Price:</span>
$549.00
</div>
<div class="price sale-price">
<span>Our Price:</span>
$489.00
</div>
</div>
I'm using the following jQuery to isolate the price values (numeric) for the sale price and regular price using "parseInt" to get rid of the text:
var regPrice = parseInt($('.regular-price').text().replace(/[^0-9.]/g, ""));
var salePrice = parseInt($('.sale-price').text().replace(/[^0-9.]/g, ""));
This is producing the values that I am expecting. Where I'm running into an issue is how to create a calculation using these var's that will loop through all ".regular-price" and ".sale-price" classes on the page and produce a percentage result.
I have tried using the "each" function to accomplish my result:
$('.sale-price').each(function(index) {
console.log( ((regPrice - salePrice) / (regPrice)) * 100%) );
});
But the console is empty.
I would just like to be able to calculate the percentage diff between the two numbers while looping through all items and write to the console until I decide where to put the value.
Thanks.