473,698 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NaN and function not returning value.

I am working on a simple orderform script to keep a running total,
however I am encountering some errors.

function CalculateTotal( ) {
var order_total = 0

// Run through all the form fields
for (var i=0; i < document.orderf orm.chkEvent.le ngth - 1; ++i) {

// Is the checkbox checked?
if (document.order form.chkEvent[i].checked) {

// If so, add value to total
order_total = (order_total + document.orderf orm.chkEvent.va lue)
//alert(order_tot al);
}
}

// Display the total rounded to two decimal places
//order_total = round_decimals( order_total, 2)
return order_total
}

when the alert is un-commented I always get Nan (not a number) how do
I convert the string value of my checkboxes to integers.

additionally later in my form when I try to call
document.write( order_total) I get nothing.

here is my test form below.

any help is greatly appreciated.

<form name="orderform " method="post">
<table width="71%" border="0">
<tr>
<td width="4%"><inp ut name="chkEvent" type="checkbox"
onClick="Calcul ateTotal()" value="50">
<td width="20%"><p> 2:15pm </p></td>
<td width="36%"><p> Poker Run </p></td>
<td width="40%"><p> $50 </p></td>
</tr>
<tr>
<td width="4%"><inp ut name="chkEvent" type="checkbox"
onClick="Calcul ateTotal()" value="10">
<td width="20%"><p> 2:15pm </p></td>
<td width="36%"><p> Poker Run </p></td>
<td width="40%"><p> $10 </p></td>
</tr>
<tr>
<td width="4%"><inp ut name="chkEvent" type="checkbox"
onClick="Calcul ateTotal()" value="50">
<td width="20%"><p> 2:15pm </p></td>
<td width="36%"><p> Poker Run </p></td>
<td width="40%"><p> $50 </p></td>
</tr>
<tr>
<td width="4%">&nbs p;</td>
<td width="20%"><p> &nbsp; </p></td>
<td width="36%"><p> <strong> Total Fees </strong></p></td>
<td width="40%">
<script>
document.writel n(order_total);
</script>

</td>
</tr>
<tr>
<td width="4%"><inp ut type="checkbox" name="chkPaymen t"
value="byCheck" ></td>
<td width="20%"><p> &nbsp; </p></td>
<td width="36%"><p> <strong> Pay By Check </strong></p></td>
<td width="40%"><p> &nbsp; </p></td>
</tr>
<tr>
<td width="4%"><inp ut type="checkbox" name="chkPaymen t"
value="AtFestiv al"></td>
<td width="20%"><p> &nbsp; </p></td>
<td width="36%"><p> <strong> Pay at the Festival
</strong></p></td>
<td width="40%"><p> &nbsp; </p></td>
</tr>
</table>
</form>
Jul 23 '05 #1
5 2248
Ron
J Lake wrote:
I am working on a simple orderform script to keep a running total,
however I am encountering some errors.

function CalculateTotal( ) {
var order_total = 0

// Run through all the form fields
for (var i=0; i < document.orderf orm.chkEvent.le ngth - 1; ++i) {

// Is the checkbox checked?
if (document.order form.chkEvent[i].checked) {

// If so, add value to total
order_total = (order_total + document.orderf orm.chkEvent.va lue)
//alert(order_tot al);
}
}

// Display the total rounded to two decimal places
//order_total = round_decimals( order_total, 2)
return order_total
}

when the alert is un-commented I always get Nan (not a number) how do
I convert the string value of my checkboxes to integers.

additionally later in my form when I try to call
document.write (order_total) I get nothing.

here is my test form below.

any help is greatly appreciated.

<form name="orderform " method="post">
<table width="71%" border="0">
<tr>
<td width="4%"><inp ut name="chkEvent" type="checkbox"
onClick="Calcu lateTotal()" value="50">
<td width="20%"><p> 2:15pm </p></td>
<td width="36%"><p> Poker Run </p></td>
<td width="40%"><p> $50 </p></td>
</tr>
<tr>
<td width="4%"><inp ut name="chkEvent" type="checkbox"
onClick="Calcu lateTotal()" value="10">
<td width="20%"><p> 2:15pm </p></td>
<td width="36%"><p> Poker Run </p></td>
<td width="40%"><p> $10 </p></td>
</tr>
<tr>
<td width="4%"><inp ut name="chkEvent" type="checkbox"
onClick="Calcu lateTotal()" value="50">
<td width="20%"><p> 2:15pm </p></td>
<td width="36%"><p> Poker Run </p></td>
<td width="40%"><p> $50 </p></td>
</tr>
<tr>
<td width="4%">&nbs p;</td>
<td width="20%"><p> &nbsp; </p></td>
<td width="36%"><p> <strong> Total Fees </strong></p></td>
<td width="40%">
<script>
document.write ln(order_total) ;
</script>

</td>
</tr>
<tr>
<td width="4%"><inp ut type="checkbox" name="chkPaymen t"
value="byCheck "></td>
<td width="20%"><p> &nbsp; </p></td>
<td width="36%"><p> <strong> Pay By Check </strong></p></td>
<td width="40%"><p> &nbsp; </p></td>
</tr>
<tr>
<td width="4%"><inp ut type="checkbox" name="chkPaymen t"
value="AtFesti val"></td>
<td width="20%"><p> &nbsp; </p></td>
<td width="36%"><p> <strong> Pay at the Festival
</strong></p></td>
<td width="40%"><p> &nbsp; </p></td>
</tr>
</table>
</form>

Note that you refer to chkEvent[i] as the checkbox and then request the
value of the array chkEvent. :) You will want to use the global function
"parseInt(docum ent.orderForm.c heckEvt[i].value)" to extract integers
from objects (or parseFloat for float values).
Jul 23 '05 #2
Lee
J Lake said:

I am working on a simple orderform script to keep a running total,
however I am encountering some errors.

function CalculateTotal( ) {
var order_total = 0

// Run through all the form fields
for (var i=0; i < document.orderf orm.chkEvent.le ngth - 1; ++i) {

// Is the checkbox checked?
if (document.order form.chkEvent[i].checked) {

// If so, add value to total
order_total = (order_total + document.orderf orm.chkEvent.va lue)
//alert(order_tot al);
}
}

// Display the total rounded to two decimal places
//order_total = round_decimals( order_total, 2)
return order_total
}

when the alert is un-commented I always get Nan (not a number) how do
I convert the string value of my checkboxes to integers.
Why not simply pass the value to your CalculateTotal( ) function,
so there is no need to convert:

onlick="Calcula teTotal(20)"

Your CalculateTotal( ) function returns the value of order_total, but
nothing is done with that returned value. It seems as if you want
to store it in a global variable, rather than return it.

What happens if the user changes his mind, and unchecks an item?

What class is this for?

additionally later in my form when I try to call
document.write (order_total) I get nothing.


Jul 23 '05 #3
On 4 May 2004 09:56:24 -0700, J Lake <ja****@idealpr omotional.com> wrote:

[snipped code]
when the alert is un-commented I always get Nan (not a number) how do
I convert the string value of my checkboxes to integers.
First of all, you need to actually get the value of the checkbox. You
don't to that. Once you have obtained the value, read the FAQ,
particularly:

<URL:http://jibbering.com/faq/#FAQ4_21>

Also, did you really intend:

for (var i=0; i < document.orderf orm.chkEvent.le ngth - 1; ++i) {

You are aware that this will omit the last checkbox, yes? I assumed that
this was a mistake in my code below.
additionally later in my form when I try to call
document.write( order_total) I get nothing.


The most compatible way of doing what you're attempting is to place a
read-only text box in the cell and write to it as usual. If you don't like
that idea, you can use the DOM to update the value. However, this is only
supported on more recent browsers.

The problem with the code you used is that global code (code that isn't
part of a function) is executed immediately. The browser will try to write
the value of order_total as the page loads. Not only is this not what you
want, but order_total is local to the CalculateTotal( ) function, so there
is no value to write anyway. Moreover, never use document.write( ) on a
page that has already loaded. You'll destroy the current page and all the
data on it. Finally, the SCRIPT element has a required type attribute.
Make sure your SCRIPT elements read

<script type="text/javascript">

and don't use the language attribute. It's deprecated as well as
unnecessary.

[snipped HTML]

A revised version of your CalculateTotal( ) function, as well as the DOM
approach to updating the total field is included below (the latter in case
you decide against using a read-only text box).

if( !document.getEl ementById ) {
if( document.all ) {
document.getEle mentById = function( id ) {
return( document.all[ id ] || null );
};
} else {
document.getEle mentById = function() {
return null;
};
}
}

/* An in-lined, and non-prototyped, version of the FAQ's
toFixed() replacement. */
function toFixed( n, p ) {
var i, s = (( n < 0 ) ? '-' : '' );
var t = String( Math.round( Math.abs( n ) * ( '1e' + p )));

if( 0 < /\D/.test( t )) {
return( s + t );
}
while( t.length < p ) {
t = '0' + t;
}
return( s + ( t.substring( 0, ( i = t.length - p )) || '0' ) + '.' +
t.substring( i ));
}

function calculateTotal( form ) {
var group = form.elements[ 'chkEvent' ], n = group.length,
total = 0;

for( var i = 0; i < n; ++i ) {
if( group[ i ].checked ) {
total += +group[ i ].value;
}
}
return total;
}

// Add units as required
function writeTotal( total, elemId ) {
var elem = document.getEle mentById( elemId );

if( elem && ( 'string' == typeof elem.innerHTML )) {
elem.innerHTML = toFixed( total, 2 );
}
}

Be aware that some of this isn't tested.

Hope it helps,
Mike

--
Michael Winter
M.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #4
On Tue, 04 May 2004 17:21:25 GMT, Ron <we*******@slid er142.com> wrote:

[snip]
Note that you refer to chkEvent[i] as the checkbox and then request the
value of the array chkEvent. :) You will want to use the global function
"parseInt(docum ent.orderForm.c heckEvt[i].value)" to extract integers
from objects (or parseFloat for float values).


Check the FAQ regarding parseInt() usage.

<URL:http://jibbering.com/faq/#FAQ4_12>

Mike
Please trim your quotes.

--
Michael Winter
M.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #5
On Tue, 04 May 2004 18:20:40 GMT, Michael Winter
<M.******@bluey onder.co.invali d> wrote:

[snip]
function calculateTotal( form ) {
var group = form.elements[ 'chkEvent' ], n = group.length,
total = 0;

for( var i = 0; i < n; ++i ) {
if( group[ i ].checked ) {
total += +group[ i ].value;
}
}
return total;
}


[snip]

I forgot to mention that this should be called by passing a reference to
the form. From the intrinsic events of form controls (which is how you
seem to call it), use

calculateTotal( this.form )

If this is inconvenient, change the first line to

var group = document.forms[ 'formName' ].elements[ 'chkEvent' ],
n = group.length;

Mike

--
Michael Winter
M.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
14027
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return value of a function acts like 'by Val' returning the value only (except for objects) can you make it return a reference instead? cheers, Krackers
9
3693
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people discuss how reflection does this, but I cannot find the syntax to do this. I have tried several code example off of gotdotnet and other articles. Can somebody please show me the code to do this?
14
1879
by: lutorm | last post by:
Hi, I'm having a problem with a return statement being parsed to return a function (I think). Check here: template <typename T> class A {}; template <typename T> class maker_of_A { public: A<T>& make_A() {return A<T>();}; };
3
2027
by: noahlt | last post by:
I'm trying to write a website updating script, but when I run the script, my function to search the DOM tree returns None instead of what it should. I have this program: -------- import sys from xml.dom.minidom import parse
1
3325
by: john | last post by:
Relatively new to C coding, so any help would greatly be appreciated. I'm having problems try to return my string array from my parsing function. When I do a printf I am getting the correct value for my first element but my subsequent printf's will return garbage. Can someone let me know what I am doing wrong. Thanks in advance. -jlewis
17
3248
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
6
2588
by: Matthew Cook | last post by:
I would like to overload the unary minus operator so that I can negate an instance of a class and pass that instance to a function without creating an explicit temporary variable. Here is an example: #include <iostream> using namespace std; class Object { public:
2
2393
by: Drew | last post by:
I am trying to build a page to generate some random data for a database that I am working on... I thought this would be simple, but it has proven to be more difficult... I am trying to create some functions to build a random date. It keeps returning a string like, "9172006//" instead of "9/17/2006". What's making this do that? Function RandomNumber(min,max) dim rand rand = Int((max-min+1)*Rnd+min) Response.Write(rand)
20
2224
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to think my way through what I want to do now. I don't know why - I'm fine with most other aspects of the language, but my brain goes numb when I'm reading about function pointers! I would like to have an array of structures, something like
5
2676
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return that structure, is this valid? As shown in the code below I am allocating the structure in the function and then returning the structure. I know if the structure contained only simple types (int, float) this will work without problems as you...
0
8603
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
9157
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
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7721
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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
4366
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
4615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1999
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.