I've made a simple javascript array of prices that I want to use all around my website via ID attributes.
var taxrate = 1.21
sp([
1.39, // size 1
1.11, // size 2
2.08, // size 3
2.09, // size 4
1.22 // size 5
]);
function sp(price) {
var i;
for (i = 0; i < price.length; i++) {
var id = "sp" + (i + 1);
document.getElementById(id).innerHTML = price[i];
var idtax = "sp" + (i + 1) + "tax";
var subtotal = (price[i] * taxrate)
var total = Math.round(subtotal*100)/100;
document.getElementById(idtax).innerHTML = total;
}
}
<span id="sp1"></span> <!-- size 1 -->
<span id="sp1tax"></span>
<span id="sp2"></span> <!-- size 2 -->
<span id="sp2tax"></span>
<span id="sp5"></span> <!-- size 5 -->
<span id="sp5tax"></span>
The problem is my webpage doesn't display values for the "size 5". I've tried to change the third span id to the "size 3" and then I get the values. If I change it back to "size 5" or "size 4", the values don't show again. If I begin with the "size 5" at the top of the list on my webpage, it get no values displayed at all. How can I display prices on my webpage using span tags in any possible ID sequence?
js var span = document.getElementById(id); console.log("span:", span);