Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #17187

Re: JavaScript Case Project 3-1: The dreaded NaN error

From Gregor Kofler <usenet@gregorkofler.com>
Newsgroups comp.lang.javascript
Subject Re: JavaScript Case Project 3-1: The dreaded NaN error
Date 2012-11-12 09:51 +0100
Organization A noiseless patient Spider
Message-ID <k7qdb9$a45$1@dont-email.me> (permalink)
References <055e7215-93ba-40a9-81f3-312ea0e4f5e2@googlegroups.com>

Show all headers | View raw


Am 2012-11-11 17:28, dmn2004404@gmail.com meinte:

[snip]

> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
> <title>Untitled Document</title>
> <script type="text/javascript">
> var purchasePrice = txtPurchasePrice.value, shippingAndHandling, totalCost;

Should already throw an error (reference error), since txtPurchasePrice 
is undefined at this point and txtPurchasePrice.value can't be evaluated.

> function calculateShipping() {

Something like

var purchasePrice = 
parseFloat(document.forms["form1"].elements["txtPurchasePrice"].value)

*in* the function should do the trick.

(Multiplying a "float-like" string with a float will also result in a 
float).


>      if (purchasePrice <= 25)
>          shippingAndHandling = 1.50;
>      else
>          shippingAndHandling = purchasePrice * .10;
>      totalCost = purchasePrice + shippingAndHandling;
> 	window.alert('Purchase price plus shipping and handling is $' + totalCost);
> }
> </script>
> </head>

> For the record, I've also tried changing "txtPurchasePrice.value" to "document.getElementById('txtPurchasePrice').value"

Depends *when* you try to reference the element.

Gregor

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 08:28 -0800
  Re: JavaScript Case Project 3-1: The dreaded NaN error "Mel Smith" <med_cutout_syntel@aol.com> - 2012-11-11 10:08 -0700
    Re: JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 09:34 -0800
      Re: JavaScript Case Project 3-1: The dreaded NaN error "Mel Smith" <med_cutout_syntel@aol.com> - 2012-11-11 10:56 -0700
        Re: JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 10:07 -0800
        Re: JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 10:52 -0800
          Re: JavaScript Case Project 3-1: The dreaded NaN error JJ <jaejunks@glegooilma-swapit.com> - 2012-11-11 20:15 +0000
            Re: JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 12:45 -0800
          Re: JavaScript Case Project 3-1: The dreaded NaN error JJ <jaejunks@glegooilma-swapit.com> - 2012-11-11 20:11 +0000
        Re: JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 12:15 -0800
          Re: JavaScript Case Project 3-1: The dreaded NaN error Osmo Saarikumpu <osmo@weppipakki.com> - 2012-11-12 10:10 +0200
  Re: JavaScript Case Project 3-1: The dreaded NaN error Gregor Kofler <usenet@gregorkofler.com> - 2012-11-12 09:51 +0100
    Re: JavaScript Case Project 3-1: The dreaded NaN error Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-12 11:49 +0100

csiph-web