0

I'm making a simple shopping cart, I have made a quantity checker with - and + buttons to change the amount desired, and also made it so it accepts only numbers in the text field. I however need to make the quantity desired match the total price displayed. and need it to update without refreshing. (eg. 1 item = $3,000,000, 2 items = $6,000,000)

Heres the javascript:

          <script type="text/javascript">
function subtractQty(){
if(document.getElementById("qty").value - 1 < 1)
return;
else
document.getElementById("qty").value--;
}

function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}



 </script>

and heres the HTML:

<form name="f1" action="" method="post">
<input type='text' name='qty' id='qty' value='1' onkeypress="return isNumber(event)" />
<input type='button' name='subtract' onclick='javascript: subtractQty();' value='-'/>
<input type='button' name='add' onclick='javascript: document.getElementById("qty").value++;' value='+'/>
<input type='submit' name='buy' value="buy">
<input type='hidden' name='product code' value="3">

</form>

http://jsfiddle.net/BRF6g/4/ - there is all the code in JSfiddle, I'm trying to get the base value of 3000000 to change every time the quantity is changed, since thats the price of the item. (note: the - button works on chrome browser, but not on this editor, for some reason)

2 Answers2

0

You can use an OnClick function.

This answers solves your problem of updating a value without page refresh.

Hope this helps.

Please do leave a feedback.

Community
  • 1
  • 1
0

This work:

<form name="f1" action="" method="post">
<input type='text' name='qty' id='qty' value='1' onkeyup="updatePrice()" />
<input type='button' name='subtract' onclick='javascript: subtractQty();updatePrice();' value='-'/>
<input type='button' name='add' onclick='javascript:document.getElementById("qty").value++;updatePrice();' value='+'/>
<input type='submit' name='buy' value="buy">
<input type='hidden' name='product code' value="3">
<span>The Cost($): <span id="total">3000000</span></span>
</form>

<script>
function subtractQty(){
if(document.getElementById("qty").value - 1 < 1)
return;
else
document.getElementById("qty").value--;
}
function updatePrice()
{
    var price = 3000000;
    var inputqty = document.getElementById("qty");
    qty = inputqty.value;
    newqty = parseInt(qty) *  parseInt(price);  
    document.getElementById("total").innerHTML = parseInt(newqty);
}
</script>