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: - <table width="500" border="1" align="center">
-
<tr bgcolor="#CCCCCC">
-
<td width="340" style="font-weight: bold">Current Housing Expenses</td>
-
<td width="144"> </td>
-
</tr>
-
<tr>
-
<td>Rent</td>
-
<td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
-
</tr>
-
<tr>
-
<td>Utilities (if paid seperate)</td>
-
<td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
-
</tr>
-
<tr bgcolor="#CCCCCC">
-
<td style="font-weight: bold">Current Non-Housing Expenses</td>
-
<td> </td>
-
</tr>
-
<tr>
-
<td>Food</td>
-
<td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
-
</tr>
-
<tr>
-
<td>Clothing</td>
-
<td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
-
</tr>
-
<tr>
-
<td>Day Care / Tuition</td>
-
<td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
-
</tr>
-
<tr>
-
<td style="font-weight: bold">Savings</td>
-
<td><input type="text" onblur="calcTotal(this, 'tot')" /></td>
-
</tr>
-
<tr bgcolor="#CCCCCC">
-
<td style="font-weight: bold">Total Monthly Expenses and Savings</td>
-
<td><input type="text" id="tot" /></td>
-
</tr>
-
</table>
Javascript code: - <script type="text/javascript">
-
function calcTotal(txtBox, totBox)
-
{
-
var totVal;
-
try
-
{
-
totVal = document.getElementById(totBox).value;
-
if(totVal!= null && totVal!='')
-
{
-
document.getElementById(totBox).value= eval(parseInt(document.getElementById(totBox).value) + parseInt(txtBox.value));
-
}
-
else
-
{
-
document.getElementById(totBox).value= txtBox.value;
-
}
-
}
-
catch(e)
-
{}
-
}
-
</script>
5 3298 gits 5,390
Recognized Expert Moderator Expert
you just need to check and replace the empty val like this: - var yourVal = '';
-
-
if (yourVal === '') {
-
yourVal = 0;
-
}
-
-
alert(parseInt(yourVal));
-
kin regards
parseInt() cant convert text to a number.
i would check first that txtBox is an integer then i would sum it all
gits 5,390
Recognized Expert Moderator Expert
exactly ... :) the problem is with the empty value ... because:
returns false so you need to check that explicitly, in case you want to use the isNaN() method.
kind regards
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,
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:
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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();
|
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...
|
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.
|
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
| |
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...
|
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">
|
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.
|
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>
|
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,...
|
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...
| |
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |