1

I've got this script which I'm using with succes, but I've tried, and failed, to get it to stop making negative numbers when substracting pass 0..

<script language="javascript" type="text/javascript">
currentvalue = 1;
function setUp() {
document.getElementById("qty").value = currentvalue;
}
function addOne() {
currentvalue++;
document.getElementById("qty").value = currentvalue;
}
function subtractOne() {
currentvalue--;
document.getElementById("qty").value = currentvalue;
}
</script>

<input type="text" name="qty" id="qty" maxlength="12" value="1" title="qyu" class="input-text qty" />
<div onload="setUp()">
<input class="qtyplus" type="button" value="" onClick="addOne()">
<input class="qtyminus" type="button" value="" onClick="subtractOne()">
</div>

Anyone got the magic part which stop's the show at 0 ?

Thanks!

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
Thomas Nielsen
  • 613
  • 2
  • 15
  • 31

1 Answers1

1

Just add an if statement here is the fiddle

and here is your subtractOne function

function subtractOne() {
    if(document.getElementById("qty").value !== '0') {
currentvalue--;
document.getElementById("qty").value = currentvalue;
    }
Kishore
  • 1,914
  • 1
  • 15
  • 22