NaN and function not returning value.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J Lake

    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>
  • Ron

    #2
    Re: NaN and function not returning value.

    J Lake wrote:
    [color=blue]
    >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>
    >
    >[/color]
    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).

    Comment

    • Lee

      #3
      Re: NaN and function not returning value.

      J Lake said:[color=blue]
      >
      >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.[/color]

      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?



      [color=blue]
      >additionally later in my form when I try to call
      >document.write (order_total) I get nothing.[/color]

      Comment

      • Michael Winter

        #4
        Re: NaN and function not returning value.

        On 4 May 2004 09:56:24 -0700, J Lake <jalake@idealpr omotional.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.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.
        [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.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.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • Michael Winter

          #5
          Re: NaN and function not returning value.

          On Tue, 04 May 2004 17:21:25 GMT, Ron <webmaster@slid er142.com> wrote:

          [snip]
          [color=blue]
          > 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).[/color]

          Check the FAQ regarding parseInt() usage.

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

          Mike


          Please trim your quotes.

          --
          Michael Winter
          M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

          Comment

          • Michael Winter

            #6
            Re: NaN and function not returning value.

            On Tue, 04 May 2004 18:20:40 GMT, Michael Winter
            <M.Winter@bluey onder.co.invali d> wrote:

            [snip]
            [color=blue]
            > 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;
            > }[/color]

            [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.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

            Comment

            Working...