JavaScript for Optimizers: Inserting a Total Discount

Jeroen Wiersma
By
Updated March 11, 2025 ·

JavaScript for Optimizers is a series of blog posts that teach you how to optimize your code, from start to finish.

In this post, we will cover one of the most important conversion rate optimization techniques: inserting a total discount on your page.

Why do you need one, you ask?

Well, you can’t go wrong with adding an attractive and informative “total discount” section on your site. It’s not only good for conversions but also great for helping your customer see just how much they can save shopping with you.

Using the existing prices on the page, we can calculate the total added value for the customer. This can then be displayed underneath the other prices.

Business Case: Inserting a Total Discount

Here’s what the end result is going to look like:

Adding the total discount of all products
Adding the total discount of all products

The Code

Did the illustration above pique your interest?

Follow the step-by-step instructions below to execute the change.

First, run the following JavaScript code on the testing page. Try it out for yourself to see it in action!

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var prices = document.querySelectorAll(".productprice");
var totalPrice = document.querySelector(".totalprice");
var discount = 0;
for (var i = 0; i < prices.length; i++) {
var cutPrices = prices[i].innerText.split(" ");
var price = Number(cutPrices[3]);
var specialprice = Number(cutPrices[5]);
var discount = discount + (price - specialprice);
}
var totalPriceElement = document.createElement("p");
var totalPriceElementNode = document.createTextNode(
"Total discount: €" + discount + ".00"
);
totalPriceElement.style.color = "green";
totalPriceElement.style.fontWeight = "bold";
totalPriceElement.appendChild(totalPriceElementNode);
totalPrice.prepend(totalPriceElement);
var prices = document.querySelectorAll(".productprice"); var totalPrice = document.querySelector(".totalprice"); var discount = 0; for (var i = 0; i < prices.length; i++) { var cutPrices = prices[i].innerText.split(" "); var price = Number(cutPrices[3]); var specialprice = Number(cutPrices[5]); var discount = discount + (price - specialprice); } var totalPriceElement = document.createElement("p"); var totalPriceElementNode = document.createTextNode( "Total discount: €" + discount + ".00" ); totalPriceElement.style.color = "green"; totalPriceElement.style.fontWeight = "bold"; totalPriceElement.appendChild(totalPriceElementNode); totalPrice.prepend(totalPriceElement);
var prices = document.querySelectorAll(".productprice");
var totalPrice = document.querySelector(".totalprice");
var discount = 0;
for (var i = 0; i < prices.length; i++) {
  var cutPrices = prices[i].innerText.split(" ");
  var price = Number(cutPrices[3]);
  var specialprice = Number(cutPrices[5]);
  var discount = discount + (price - specialprice);
}
var totalPriceElement = document.createElement("p");
var totalPriceElementNode = document.createTextNode(
  "Total discount: €" + discount + ".00"
);
totalPriceElement.style.color = "green";
totalPriceElement.style.fontWeight = "bold";
totalPriceElement.appendChild(totalPriceElementNode);
totalPrice.prepend(totalPriceElement);
lear how you can use javascript to develop test for a/b testing tool

The Logic

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var prices = document.querySelectorAll(".productprice");
var prices = document.querySelectorAll(".productprice");
var prices = document.querySelectorAll(".productprice");

We start of by assigning all the product prices with the class “productprice” to a variable.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var totalPrice = document.querySelector(".totalprice");
var totalPrice = document.querySelector(".totalprice");
var totalPrice = document.querySelector(".totalprice");

Then, assign the current totalprice to the totalPrice variable.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var discount = 0;
var discount = 0;
var discount = 0;

We need to declare a discount variable that starts at 0 so we can add a number every time the loop runs.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
for (var i = 0; i < prices.length; i++) {
for (var i = 0; i < prices.length; i++) {
for (var i = 0; i < prices.length; i++) {

Then, start a loop that runs the length of the prices array we assigned earlier.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var cutPrices = prices[i].innerText.split(" ");
var cutPrices = prices[i].innerText.split(" ");
var cutPrices = prices[i].innerText.split(" ");

Save the entire price string in the array. This means that we need to split the string for the part that we need. This will split the string into different parts and save it into an array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var price = Number(cutPrices[3]);
var price = Number(cutPrices[3]);
var price = Number(cutPrices[3]);

At index 3 of this array, we find the price before the discount. Immediately convert this to a number.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var specialprice = Number(cutPrices[5]);
var specialprice = Number(cutPrices[5]);
var specialprice = Number(cutPrices[5]);

At index 5 of this array, we find the price after the discount. Once again, immediately convert this to a number.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var discount = discount + (price - specialprice);
var discount = discount + (price - specialprice);
var discount = discount + (price - specialprice);

Then, calculate the price difference between those prices and add it to the discount variable. This loop will end when we have the total discount added.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
}
}
}

For the next step, close out the loop.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var totalPriceElement = document.createElement("p");
var totalPriceElement = document.createElement("p");
var totalPriceElement = document.createElement("p");

Next, we need a new paragraph element in order to append our string with the correct formatting.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var totalPriceElementNode = document.createTextNode(
"Total discount: €" + discount + ".00"
);
var totalPriceElementNode = document.createTextNode( "Total discount: €" + discount + ".00" );
var totalPriceElementNode = document.createTextNode(
"Total discount: €" + discount + ".00"
);

This paragraph will contain a text node with the text we want to show.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
totalPriceElement.style.color = "green";
totalPriceElement.style.color = "green";
totalPriceElement.style.color = "green";

Give the paragraph a green color to signal a positive number.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
totalPriceElement.style.fontWeight = "bold";
totalPriceElement.style.fontWeight = "bold";
totalPriceElement.style.fontWeight = "bold";

Add a bold font weight, to make it stand out more.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
totalPriceElement.appendChild(totalPriceElementNode);
totalPriceElement.appendChild(totalPriceElementNode);
totalPriceElement.appendChild(totalPriceElementNode);

Then, add the textnode to our paragraph element.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
totalPrice.prepend(totalPriceElement);
totalPrice.prepend(totalPriceElement);
totalPrice.prepend(totalPriceElement);

Finally, add our total discount to the DOM.

Good luck with testing! Check out the previous episode if you want to learn how to add a back-to-top button or our article on adding a date-time picker. You can also follow me on LinkedIn for weekly content about A/B testing with JavaScript.

Mobile reading? Scan this QR code and take this blog with you, wherever you go.
Originally published June 23, 2021 - Updated March 11, 2025
Written By
Jeroen Wiersma
Jeroen Wiersma
Jeroen Wiersma
Jeroen Wiersma is a technical marketeer and founder of GrowthPenguin.
Edited By
Carmen Apostu
Carmen Apostu
Carmen Apostu
Head of Marketing at Convert
Start your 15-day free trial now.
  • No credit card needed
  • Access to premium features
You can always change your preferences later.
You're Almost Done.
What Job(s) Do You Do at Work? * (Choose Up to 2 Options):
Convert is committed to protecting your privacy.

Important. Please Read.

  • Check your inbox for the password to Convert’s trial account.
  • Log in using the link provided in that email.

This sign up flow is built for maximum security. You’re worth it!