sathyadev
sathyadev

Reputation: 43

change the subtotal dynamically when the quantity changes in shopping cart

I just want the javascript code to achieve this simple thing. The subtotal price should be changed according to the quantity the user changes. If the price is 1000 and the quantity is changed to 2 the subtotal should be 2000.

Here is My code :

<input type="number" min="0" class="form-control" id="quantity" value="1">
</td>
<td>
    <input type="text" class="form-control" id="price" value="1000" readonly>
</td>
<td>
    <input type="text" class="form-control" id="subTotal" value="1000">
</td>

I'm zero in javascript. also please show me the way to add the script inside my HTML.

Upvotes: 1

Views: 3294

Answers (2)

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28445

$(document).ready(function(){
    $("#quantity").change(function(){
        $("#subTotal").val(parseInt($(this).val()) * parseInt($("#price").val()));
    });
});

I have added a plunker for you - http://plnkr.co/edit/aF6G1SSJZTLREH6cBBul

Firstly, add your code in document ready block as it executes your code only after the document is ready. Secondly, bind the onchange event with input field with id quantity and do all the calculations. Please note, you should also do some validations, like if a word is entered or the filed is left blank, etc.

Upvotes: 3

Helmut Granda
Helmut Granda

Reputation: 4665

I would recommend starting from the basics if you want to do this often:

http://www.codecademy.com/en/tracks/javascript

If it is just a one time thing you can hire some one to do it for you.

Upvotes: -1

Related Questions