I have a list of products in my store. Each product has a price in a div. I need to find the price of each product, and calculate a sales price of 30%, and append it to the price in red.
I can do the DOM manipulation for this, but I cant figure out how to loop over the products. This code below appends the sales price of ONLY the first product to all of the products in the list.
Working Fiddle: https://jsfiddle.net/m5fxnLqp/1/
Sample HTML:
<ul class="ProductList">
<li class="product">
<div class="ProductPrice">$9.99</div>
</li>
<li class="product">
<div class="ProductPrice">$10.99</div>
</li>
</ul>
Here is the DOM Manipulation:
//find the price and convert to decimal to work with
var price = $(".ProductPrice").text();
var priceFormat = price.replace("$", "");
var priceNum = parseFloat(priceFormat).toFixed(2);
//calculate the price discount
var priceDiscount = priceNum * .30;
var priceFinal = priceDiscount.toFixed(2);
// Append the formatted price to the div
var formattedPrice = '<div style="color:red;"> $' + priceFinal + '</div>';
$(".ProductPrice").append( formattedPrice );