-6

I have two textbox as follow:

<input id="amount" name="amount" type="text" value="0" />
<input id="discount" name="discount" type="text" value="0" />

I want substract discount from amount, and add result to another textbox. How can I do it?

Elvin Mammadov
  • 25,329
  • 11
  • 40
  • 82

2 Answers2

1

You can do this using JavaScript/Jquery

Suppose your third textbox is

Then your Jquery code will be

var vResult = parseInt( $('#amount').val() )  -  parseInt( $('#discount').val() );
$('#result').val( vResult );
Sudz
  • 4,268
  • 2
  • 17
  • 26
1
$("#result").val(parseInt($("#amount").val(), 10) - parseInt($("#discount").val(), 10));

That should do it. jsFiddle here.

Jonathan
  • 1,923
  • 2
  • 16
  • 34
  • yor code doesn't work in modal popup. I have many input text there. – Elvin Mammadov May 06 '13 at 12:25
  • 1
    Can you post a jsFiddle example? – Jonathan May 06 '13 at 12:27
  • We use primary template for our website, so i can't create such jsFiddle example. But I can explain my situation. I have contract View in mvc application, and when i click button, adding modal popup shown and there are many textboxes,(contract number, company, client etc.), include amount, discount, total amount textboxes. I write your script near their. But in total textbox I get nan value. – Elvin Mammadov May 06 '13 at 12:41
  • 1
    If your text boxes have any non numbers in them, you will get `NaN`. If you are dealing with money, try this instead of using `parseInt` http://stackoverflow.com/a/559178/804665 – Jonathan May 06 '13 at 12:47