472,126 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 software developers and data experts.

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

// Is the checkbox checked?
if (document.orderform.chkEvent[i].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>
Jul 23 '05 #1
5 2153
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.orderform.chkEvent.length - 1; ++i) {

// Is the checkbox checked?
if (document.orderform.chkEvent[i].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[i] as the checkbox and then request the
value of the array chkEvent. :) You will want to use the global function
"parseInt(document.orderForm.checkEvt[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.orderform.chkEvent.length - 1; ++i) {

// Is the checkbox checked?
if (document.orderform.chkEvent[i].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?

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****@idealpromotional.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.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

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #4
On Tue, 04 May 2004 17:21:25 GMT, Ron <we*******@slider142.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(document.orderForm.checkEvt[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.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #5
On Tue, 04 May 2004 18:20:40 GMT, Michael Winter
<M.******@blueyonder.co.invalid> 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.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Derek Hart | last post: by
14 posts views Thread by lutorm | last post: by
3 posts views Thread by noahlt | last post: by
6 posts views Thread by Matthew Cook | last post: by
20 posts views Thread by MikeC | last post: by

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.