NaN and function not returning value.

J

J Lake

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.orderform.chkEvent.length - 1; ++i) {

// Is the checkbox checked?
if (document.orderform.chkEvent.checked) {

// If so, add value to total
order_total = (order_total + document.orderform.chkEvent.value)
//alert(order_total);
}
}

// 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%"><input name="chkEvent" type="checkbox"
onClick="CalculateTotal()" 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%"><input name="chkEvent" type="checkbox"
onClick="CalculateTotal()" 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%"><input name="chkEvent" type="checkbox"
onClick="CalculateTotal()" 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%">&nbsp;</td>
<td width="20%"><p>&nbsp; </p></td>
<td width="36%"><p><strong> Total Fees </strong></p></td>
<td width="40%">
<script>
document.writeln(order_total);
</script>

</td>
</tr>
<tr>
<td width="4%"><input type="checkbox" name="chkPayment"
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%"><input type="checkbox" name="chkPayment"
value="AtFestival"></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>
 
R

Ron

J 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.orderform.chkEvent.length - 1; ++i) {

// Is the checkbox checked?
if (document.orderform.chkEvent.checked) {

// If so, add value to total
order_total = (order_total + document.orderform.chkEvent.value)
//alert(order_total);
}
}

// 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%"><input name="chkEvent" type="checkbox"
onClick="CalculateTotal()" 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%"><input name="chkEvent" type="checkbox"
onClick="CalculateTotal()" 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%"><input name="chkEvent" type="checkbox"
onClick="CalculateTotal()" 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%">&nbsp;</td>
<td width="20%"><p>&nbsp; </p></td>
<td width="36%"><p><strong> Total Fees </strong></p></td>
<td width="40%">
<script>
document.writeln(order_total);
</script>

</td>
</tr>
<tr>
<td width="4%"><input type="checkbox" name="chkPayment"
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%"><input type="checkbox" name="chkPayment"
value="AtFestival"></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 as the checkbox and then request the
value of the array chkEvent. :) You will want to use the global function
"parseInt(document.orderForm.checkEvt.value)" to extract integers
from objects (or parseFloat for float values).
 
L

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.orderform.chkEvent.length - 1; ++i) {

// Is the checkbox checked?
if (document.orderform.chkEvent.checked) {

// If so, add value to total
order_total = (order_total + document.orderform.chkEvent.value)
//alert(order_total);
}
}

// 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="CalculateTotal(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?
 
M

Michael Winter

[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.orderform.chkEvent.length - 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.getElementById ) {
if( document.all ) {
document.getElementById = function( id ) {
return( document.all[ id ] || null );
};
} else {
document.getElementById = 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.getElementById( 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
 
M

Michael Winter

[snip]
Note that you refer to chkEvent as the checkbox and then request the
value of the array chkEvent. :) You will want to use the global function
"parseInt(document.orderForm.checkEvt.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.
 
M

Michael Winter

On Tue, 04 May 2004 18:20:40 GMT, Michael Winter

[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top