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)

11
  • 1
    That's one expensive item :) Commented Apr 10, 2014 at 8:52
  • 1
    hehe, yeh im making an imaginary 'flying carpet' sales page. 3,000,000 is pretty reasonable for a flying carpet i say :P Commented Apr 10, 2014 at 8:54
  • You have to bind to the onChange event of your quantity element Commented Apr 10, 2014 at 8:56
  • You don't need javascript: in onXXX attributes. You only need it when you're putting JS in href. Commented Apr 10, 2014 at 8:58
  • Where is the total price in your HTML? Commented Apr 10, 2014 at 9:00

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.