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.
$(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.