Does the following help:
var currentMode = 1;
var discount = 0.20;
var reverseDiscount = 1/(1-discount);
function togglePrices() {
var prices = document.getElementsByClassName("price");
for (var i = 0; i < prices.length; i++) {
var individualPrice = prices[i].innerHTML.substring(1);
if(currentMode == 1) {
individualPrice = parseFloat(individualPrice) * (1-discount);
} else {
individualPrice = parseFloat(individualPrice) * reverseDiscount;
}
prices[i].innerHTML = "$" + individualPrice;
}
currentMode = (++currentMode) % 2;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table border="1">
<tr>
<td>1</td>
<td>Item 1</td>
<td><span class="price" >$40</span></td>
</tr>
<tr>
<td>2</td>
<td>Item 2</td>
<td><span class="price" >$20</span></td>
</tr>
<tr>
<td>3</td>
<td>Item 3</td>
<td><span class="price" >$50</span></td>
</tr>
</table>
</body>
<button type="button" onclick="togglePrices()">Toggle Discounts</button>
</body>
</html>
2
solved Change all prices (numbers) with 20% on webpage [closed]