473,815 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript: Column total

2 New Member
Hi all,

I've created a table and it works just fine when you insert values in the text field. When you insert "0" as a value it still works, problem is if you skip a text field and don't insert anything it returns "NaN" in the total column. I can't figure out what to change. Any help would be much apprecitated.

Also, and this would be a bonus, how do you insert a button to clear the entire field to start over if you wanted to without having to delete every entry.

Thanks,



HTML Code:

Expand|Select|Wrap|Line Numbers
  1. <table width="500" border="1" align="center">
  2.         <tr bgcolor="#CCCCCC">
  3.           <td width="340" style="font-weight: bold">Current Housing Expenses</td>
  4.           <td width="144">&nbsp;</td>
  5.         </tr>
  6.         <tr>
  7.           <td>Rent</td>
  8.           <td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
  9.         </tr>
  10.         <tr>
  11.           <td>Utilities (if paid seperate)</td>
  12.           <td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
  13.         </tr>
  14.         <tr bgcolor="#CCCCCC">
  15.           <td style="font-weight: bold">Current Non-Housing Expenses</td>
  16.           <td>&nbsp;</td>
  17.         </tr>
  18.         <tr>
  19.           <td>Food</td>
  20.           <td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
  21.         </tr>
  22.         <tr>
  23.           <td>Clothing</td>
  24.           <td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
  25.         </tr>
  26.         <tr>
  27.           <td>Day Care / Tuition</td>
  28.           <td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
  29.         </tr>
  30.         <tr>
  31.           <td style="font-weight: bold">Savings</td>
  32.           <td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
  33.         </tr>
  34.         <tr bgcolor="#CCCCCC">
  35.           <td style="font-weight: bold">Total Monthly Expenses and Savings</td>
  36.           <td><input type="text" id="tot" /></td>
  37.         </tr>
  38.       </table>
Javascript code:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript"> 
  2. function calcTotal(txtBox, totBox) 
  3.     var totVal; 
  4.     try 
  5.     {  
  6.     totVal = document.getElementById(totBox).value; 
  7.     if(totVal!= null && totVal!='') 
  8.      { 
  9.        document.getElementById(totBox).value= eval(parseInt(document.getElementById(totBox).value) + parseInt(txtBox.value));    
  10.      } 
  11.      else 
  12.      { 
  13.         document.getElementById(totBox).value= txtBox.value;            
  14.      } 
  15.     } 
  16.     catch(e) 
  17.     {} 
  18. </script>
Mar 27 '09 #1
5 3298
gits
5,390 Recognized Expert Moderator Expert
you just need to check and replace the empty val like this:

Expand|Select|Wrap|Line Numbers
  1. var yourVal = '';
  2.  
  3. if (yourVal === '') {
  4.    yourVal = 0;
  5. }
  6.  
  7. alert(parseInt(yourVal));
  8.  
kin regards
Mar 28 '09 #2
Amzul
130 New Member
parseInt() cant convert text to a number.
i would check first that txtBox is an integer then i would sum it all
Mar 28 '09 #3
gits
5,390 Recognized Expert Moderator Expert
exactly ... :) the problem is with the empty value ... because:

Expand|Select|Wrap|Line Numbers
  1. isNaN('')
returns false so you need to check that explicitly, in case you want to use the isNaN() method.

kind regards
Mar 28 '09 #4
mrkh
2 New Member
thanks for the reply gits, however I am very new to making websites actually I'm a student that just received my certifications. I have no problem with the content of the website and making the tables and etc., my problem is the javascript. Totally lost, based on your reply I don't know where to insert or replace my existing code.

Thanks,
Mar 29 '09 #5
gits
5,390 Recognized Expert Moderator Expert
as you may see in the example you should check the value that you want to parse to an integer right before you want to use the parseInt() method, because in case you would try:

Expand|Select|Wrap|Line Numbers
  1. parseInt('');
you will get NaN as the result. so you need to check and replace the value that you want to parse before you use parseInt() ... ok?

kind regards
Mar 30 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

53
5758
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is difficult to know what is going on. One of these Order Forms you can see here... http://www.cardman.co.uk/orderform.php3
1
2214
by: Terry | last post by:
All I'm doing, is using the Compute Method and getting the Sum of the Amount field in my SQL Query. The problem is getting the Data Table to add a Column and put the total in the "Total Column". I can see that I'm getting the data I want, But can not display it. I could sure use the Help. Thanks In Advance THE CODE: SqlDataAdapter da = new SqlDataAdapter(); DataSet ds = new DataSet(); this.sqlConnection1.Open();
1
2304
by: Steve | last post by:
I have looked through the newsgroup for an answer to this but haven't been able to find anything resembling my situation. What I want to do is relatively simple, I think. I have a crosstab query that is bound to a report I am creating. I would like the column headings to be the name of the correspoding field name. For example, I have field names ALS, BCD, HLP, and the detail section prints the values for these fields. I would like...
5
1500
by: perspolis | last post by:
hi everyone I used a column expression like "UnitPrice*Quantity" for a cloumn named Total. but when I want to update my dataset by SqlDataAdopter, it gives me error. The column Total is just for showing "UnitPrice*Quantity" not for inserting in database,but I don't know why it gives me SystemException error. When I remove column expression and calculate "UnitPrice*Quantity" programmatically ,it works well.
24
6348
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to have on his site, a Javascript Calculator for working out the cost of what they want, for example: 1 widget and 2 widglets = £5.00
4
2202
by: drago | last post by:
Hi guys, I will be quick this time... Just want to know how we can calculate generic values in the same column. I am using invoice form with a services subform . I have a column of 'total' in subform and I want to add all values of that column to get the total cost on main invoice form. The 'total' column contains service cost of different clients. Some clients have 1 service i.e 1 total which is the total cost but most clients have more then 1...
0
1188
parshupooja
by: parshupooja | last post by:
Hi I am using asp.net C# and javascript. I have a gridview with 5 columns and 14 row column1--column2--column3--column4--column5 Column1 to column4 is textboxes wheresa column5 is label I am trying to calculate column 5 which is (column2-column1)+(column4-column3) Here is javascript i am using. When i use this it just calculates for first row of gridview. <script language =javascript type="text/javascript">
1
1945
by: sheldonlg | last post by:
I have inherited code with a TDC control. In this file, there are two javascripts of interest. One of these is a function, filter(), which is inside <script language=javascript></script>. The other is code inside <script language=javascript FOR=inquiry event=ondatasetcomplete()></script> where inquiry is the TDC.
13
24471
by: dmj07 | last post by:
Hi all, I have a simple table set up in which a user can insert values into the boxes and onkeydown it will sum up the column. Table: <table> <tr> <td><input type="text" onkeydown="calcTotal(this, 'tot')" /></td>
0
9613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10673
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10408
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6899
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5712
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4361
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3889
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3033
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.