hi i am having problem in adding three textbox values into 4th textbox say txt1,txt2,txt3 into txt4 on keypress .. using jquery .. .can someone help me out please ??? how to proceed about it :( the problem is in case i hav put two textbox values.. and left
the 3rd textbox .. like for example ...
txt 1 = 3
txt 2 = 4
txt 3 = ""
then its giving the result as NaN.... how to deal with this situation that on keypress i may add the value .. but irrespective or blank values in textbox it would add the value and show us the total.... in txt 4 .....
hii there thanks .. it really worked ... interesting thing is .. i can even work out this on my gridview textboxes... when they hav a common id :) ... both answers helped me out :)
Thanks & Regards
Joy
Marked as answer by joydeep1985 on Jan 29, 2011 12:17 PM
joydeep1985
Member
117 Points
104 Posts
problem in adding three textbox values into 4th textbox on keypress using jquery
Jan 29, 2011 03:16 AM|LINK
hi i am having problem in adding three textbox values into 4th textbox say txt1,txt2,txt3 into txt4 on keypress .. using jquery .. .can someone help me out please ??? how to proceed about it :( the problem is in case i hav put two textbox values.. and left the 3rd textbox .. like for example ...
txt 1 = 3
txt 2 = 4
txt 3 = ""
then its giving the result as NaN.... how to deal with this situation that on keypress i may add the value .. but irrespective or blank values in textbox it would add the value and show us the total.... in txt 4 .....
jquery asp.net
Joy
raghav_khung...
All-Star
32835 Points
5563 Posts
MVP
Re: problem in adding three textbox values into 4th textbox on keypress using jquery
Jan 29, 2011 05:18 AM|LINK
Live: http://jsbin.com/ivige4 It will work both for int and decimals
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> <script type="text/javascript"> $(function() { var textBox1 = $('input:text[id$=TextBox1]').keyup(foo); var textBox2 = $('input:text[id$=TextBox2]').keyup(foo); var textBox3 = $('input:text[id$=TextBox3]').keyup(foo); function foo() { var value1 = textBox1.val(); var value2 = textBox2.val(); var value3 = textBox3.val(); var sum = add(value1, value2, value3); $('input:text[id$=TextBox4]').val(sum); } function add() { var sum = 0; for (var i = 0, j = arguments.length; i < j; i++) { if (IsNumeric(arguments[i])) { sum += parseFloat(arguments[i]); } } return sum; } function IsNumeric(input) { return (input - 0) == input && input.length > 0; } }); </script> </head> <body> <form id="form1" runat="server"> <asp:TextBox runat="server" ID="TextBox1"></asp:TextBox> <asp:TextBox runat="server" ID="TextBox2"></asp:TextBox> <asp:TextBox runat="server" ID="TextBox3"></asp:TextBox> <asp:TextBox runat="server" ID="TextBox4"></asp:TextBox> </form> </body> </html>asteranup
All-Star
30184 Points
4906 Posts
Re: problem in adding three textbox values into 4th textbox on keypress using jquery
Jan 29, 2011 08:06 AM|LINK
Hi,
See the below code-
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> <script src="scripts/jquery.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { $(".toCalculate").keyup(function() { var total = 0; $(".toCalculate").each(function(index, item) { temp = parseFloat($(item).val()); if (isNaN(temp)) temp = 0; total = total + temp; }); $(".total").val(total); }); }); </script> </head> <body> <form id="form1" runat="server"> <asp:TextBox ID="TextBox1" runat="server" CssClass="toCalculate"></asp:TextBox> <asp:TextBox ID="TextBox2" runat="server" CssClass="toCalculate"></asp:TextBox> <asp:TextBox ID="TextBox3" runat="server" CssClass="toCalculate"></asp:TextBox> <asp:TextBox ID="TextBox4" runat="server" CssClass="total"></asp:TextBox> </form> </body> </html>Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
joydeep1985
Member
117 Points
104 Posts
Re: problem in adding three textbox values into 4th textbox on keypress using jquery
Jan 29, 2011 12:16 PM|LINK
hii there thanks .. it really worked ... interesting thing is .. i can even work out this on my gridview textboxes... when they hav a common id :) ... both answers helped me out :)
Joy