i already have this code but it does not work as expected.
the word "piece" should be changed to "pieces" when the quantity is greater than 1.
<span class="counter b">2 <span id="unitpcs"></span></span>
<script type="text/javascript">
var q = document.getElementsByTagName("span");
var a = myFunction(q.innerHTML);
document.getElementById("unitpcs").innerHTML = x;
function myFunction() {
if (a > 1) {
x = "pieces";
} else {
x = "piece";
}
}
</script>
Currently, when the quantity is equal to one (1), the result is correct: 1 piece.
If the quantity becomes two (2), the result is not correct: 2 piece The correct output should be "2 pieces".
Any solution is highly appreciated. Thanks in advance!
a
as the parameter of myFunction()
.q
is an array of two span
elements..innerHTML
to a number. <span class="counter b">2 <span id="unitpcs"></span></span>
<script type="text/javascript">
var q = document.getElementsByTagName("span");
myFunction(parseFloat(q[0].innerHTML)); // first span, convert to number
document.getElementById("unitpcs").innerHTML = x;
function myFunction(a) {
if (a > 1) {
x = "items";
} else {
x = "item";
}
}
</script>