On 4 May 2004 09:56:24 -0700, J Lake <jalake@idealpromotional.com> wrote:
[snipped code]
[color=blue]
> 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.[/color]
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.
[color=blue]
> additionally later in my form when I try to call
> document.write(order_total) I get nothing.[/color]
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.Winter@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)