2

i want to have a form whereby i can add a value into the first text field, multiply that by a second hidden field, and display results in a 3rd text field. I want it so as soon as you add a value to the first field it shows the result in the 3rd field, eg on key up.

<label>Qty:</label>
<input name="qty" id="qty" type="text" />
<input name="price" id="price" type="hidden" value="30" /><br />

<label>Total:</label>
<input name="total" id="total" type="text" />

Any help would be great as im new to jquery.

2
  • Have a read in the jQuery API about the keypress handler, as well as the val() function. Other than that, its basic maths. Commented Aug 18, 2013 at 14:41
  • in first text you want like '1+1' or what ? Commented Aug 18, 2013 at 14:45

3 Answers 3

4

Though you got the answer but you should type cast the values you type in inputs. I mean what if someone type any other value other than integer... then you will get NaN in 3rd textbox..Here is solution to handle this problem

$(document).ready(function(){
    var qty=$("#qty");
    qty.keyup(function(){
        var total=isNaN(parseInt(qty.val()* $("#price").val())) ? 0 :(qty.val()* $("#price").val())
        $("#total").val(total);
    });
});

or you can check jsfiddle http://jsfiddle.net/ugPxf/

Sign up to request clarification or add additional context in comments.

1 Comment

Can you help me to make this calculator? services.resellerbytes.com/business-email
4
$("#qty").keyup(function(){
   total = $("#qty").val()* $("#price").val();
   $("#total").val(total);
});

2 Comments

and thanks to Akhil5, your solution worked too. so easy when you know how :)
Please try to explain why your solution works as well as giving the code.
0

This should work.

$('#qty').on('keyup',function(){
     $('#total').val( $('#qty').val() * $('#price').val());
});

R.

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.