I’ve got a javascript function that adds one to a quantity if the current value is less than another value. Here is the javascript function:
function addQty()
{
if(document.getElementById("quantity").value < document.getElementById("stock").value)
document.getElementById("quantity").value++;
else
return;
}
And here are the form elements that the values are taken from:
<input type='text' id="quantity" name='quantity' value ='0' />
<input type='hidden' id="stock" name="stock" value="<?php echo $adjustedStock; ?>" />
So basically the user can add one to their quantity of a product to order only if there is enough in stock.
Now, this works absolutely fine if the number in stock is 1-9, but if the stock level is in double digits, the maximum the user can add to their basket (ie the ‘quantity’ in the code) returns as the first digit + 1. So if the stock is 13 then the max quantity is 2, or if the stock is 63 the max quantity is 7.
I’ve tried converting the integer value of $adjustedStock to a string before it is used in the form as I read sometimes browsers can behave weirdly in this situation, but this didn’t work. Any ideas why this is happening?
Thanks in advance!
Form element values are strings. Use
parseIntor convert to a number some other way.