1

I have this in my Form

 <%= f.label :price, "price"%> 
<%= f.text_field :price, :size=>20, :id =>"price" %>

<%= f.label :quantity, "Quantity"%>
<%= f.text_field :quantity, :size=>20, :id =>"qty" %>

<%= f.label :amount, "Amount"%>
<%= f.text_field :amount, :size=>20, :id =>"amount"%>

I want {price*quantity} to happen inside the 'amount' field as soon as i enter values inside 'price' and 'quantity' i.e calculations should happen before submit. I am new to jQuery/Ajax, so any help will do. Thanks in advance.

bharath
  • 623
  • 7
  • 30
  • 1
    @Yorgo ready learn ruby and you'll see HTML where you've never seen HTML. – noob Apr 20 '12 at 11:59
  • @micha you can see created html in browser. For example in internet explorer you can see in Developer tools (F12) – Yorgo Apr 20 '12 at 12:21
  • @Yorgo yes but it's not needed because we've got ruby code. By the Way IE is a bad example. – noob Apr 20 '12 at 12:30
  • @micha i asked for html codes because i dont know the ruby notation. So its needed for me and why IE is bad example? – Yorgo Apr 20 '12 at 12:32
  • @Yorgo Because the Developer tools of Chrome and Firefox are much better and btw even IE10 don't support the File API. So I don't recommend to use IE. – noob Apr 20 '12 at 12:37
  • @micha its your opinion. i dont think so – Yorgo Apr 20 '12 at 12:39

1 Answers1

4

You can do something like this

$(function() {
    $("#price, #qty").keyup(function() {
        var p = $("#price").val();
        var q = $("#qty").val();
        $("#amount").val(q * p);
    });
});

But you have to give the amount field the id "amount" and not reuse "price" as the id.

thomas
  • 2,580
  • 1
  • 22
  • 28
  • I am getting 'NaN' in my amount field. Where m i going wrong? – bharath Apr 21 '12 at 04:48
  • Got it. had a spelling mistake and since i m using jquery i was advised to use `jQuery` instead of `$`. working wonderfully. :) Thanks alot mate..!! – bharath Apr 21 '12 at 05:44