Substraction between two value with jquery keypress event. [Resolved]

Posted by Rashedbin under jQuery on 5/18/2015 | Points: 10 | Views : 439 | Status : [Member] | Replies : 3
Hi ! i want to substract textbox2 value from textbox1 and display result in textbox3 in asp.net with jquery.

i write the following code on textbox2 keypress

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#TextBox2").keydown(function () {
var a = $("#TextBox1").val();

var b = $("#TextBox2").val();

var c = a - b;

$("#TextBox3").val(c);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" class="Panel" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</asp:Panel>
</div>
</form>


this working but not properly. its result such as

40 - 10 = 39(e.i when i press 3 it does not fire,when press zero then substract 40 - 1 not 40 - 10)
400 - 100 = 390
400 - 10 = 300
please give me a solution.thanks all.




Responses

Posted by: Rashedbin on: 5/18/2015 [Member] Starter | Points: 25

Up
0
Down

Resolved
thank you very much.it's fine.

Rashedbin, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sheonarayan on: 5/18/2015 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
You will need to parse the value of textboxes to integer like below and also use keyUp event instead of keyDown.

var a = parseInt($("#TextBox1").val());

same way, do for variable b also and it should work.

See the demo here http://techfunda.com/Code/Demo?demoid=24

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Rashedbin, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rashedbin on: 5/18/2015 [Member] Starter | Points: 25

Up
0
Down
right answer.it's working properly.

Rashedbin, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response