Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #17166 > unrolled thread
| Started by | dmn2004404@gmail.com |
|---|---|
| First post | 2012-11-11 08:28 -0800 |
| Last post | 2012-11-12 11:49 +0100 |
| Articles | 13 — 6 participants |
Back to article view | Back to comp.lang.javascript
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
| From | dmn2004404@gmail.com |
|---|---|
| Date | 2012-11-11 08:28 -0800 |
| Subject | JavaScript Case Project 3-1: The dreaded NaN error |
| Message-ID | <055e7215-93ba-40a9-81f3-312ea0e4f5e2@googlegroups.com> |
I'm trying to work on Case Project 3-1 in the book "JavaScript" by Don Gosselin. I have almost everything down, including the alert box in the end, but no matter what I type in the text box where I'm supposed to enter a purchase price, the function that is supposed to calculate the shipping and the total cost returns the dreaded NaN error, and the alert box reflects the NaN error. Upon further investigation, I suspect that somehow my function is not recognizing that there IS a numeric value in the text box -- I think for some reason it's reading it as a string. Here's the code I have for my page. Can someone out there examine it and tell me where I'm going wrong?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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;
function calculateShipping() {
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>
<body>
Purchase price (no dollar signs):
<form id="form1" method="post" action="">
<p>
<label for="txtPurchasePrice"></label>
<input type="text" name="txtPurchasePrice" id="txtPurchasePrice" />
<input type="submit" name="btnCalculate" id="btnCalculate" value="Calculate" onclick="calculateShipping()" />
</p>
</form>
</body>
</html>
For the record, I've also tried changing "txtPurchasePrice.value" to "document.getElementById('txtPurchasePrice').value" and even "txtPurchasePrice.text", but none of what I'm doing appears to make any difference -- no matter which way I go, it still says NaN.
[toc] | [next] | [standalone]
| From | "Mel Smith" <med_cutout_syntel@aol.com> |
|---|---|
| Date | 2012-11-11 10:08 -0700 |
| Message-ID | <aga4cnF1f0vU1@mid.individual.net> |
| In reply to | #17166 |
where I'm going wrong?
You must convert the text string input to a floating point number --
probably using the parseFloat() function. See below
-Mel Smith (one neophyte to another)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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;
function calculateShipping() {
// in here you need to convert the txtPurchasePrice to a floating point
numeric before
// you can start your calculations. Perhaps like the following:
purchasePrice =
parseFloat(document.getElementById("txtPurchasePrice").value) ;
if (purchasePrice <= 25)
shippingAndHandling = 1.50;
else
shippingAndHandling = purchasePrice * .10;
totalCost = purchasePrice + shippingAndHandling;
// now you should format your totalCost to an appropriate number of
digits below
window.alert('Purchase price plus shipping and handling is $' + totalCost);
}
</script>
</head>
<body>
Purchase price (no dollar signs):
<form id="form1" method="post" action="">
<p>
<label for="txtPurchasePrice"></label>
<input type="text" name="txtPurchasePrice" id="txtPurchasePrice" />
<input type="submit" name="btnCalculate" id="btnCalculate"
value="Calculate" onclick="calculateShipping()" />
</p>
</form>
</body>
</html>
For the record, I've also tried changing "txtPurchasePrice.value" to
"document.getElementById('txtPurchasePrice').value" and even
"txtPurchasePrice.text", but none of what I'm doing appears to make any
difference -- no matter which way I go, it still says NaN.
[toc] | [prev] | [next] | [standalone]
| From | dmn2004404@gmail.com |
|---|---|
| Date | 2012-11-11 09:34 -0800 |
| Message-ID | <f361ca82-911a-4f52-9132-3f0af5baf3ca@googlegroups.com> |
| In reply to | #17168 |
On Sunday, November 11, 2012 11:08:42 AM UTC-6, Mel Smith wrote:
> where I'm going wrong?
>
>
>
> You must convert the text string input to a floating point number --
>
> probably using the parseFloat() function. See below
>
>
>
> -Mel Smith (one neophyte to another)
>
>
>
>
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
>
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
>
> <html xmlns="http://www.w3.org/1999/xhtml">
>
> <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;
>
> function calculateShipping() {
>
>
>
> // in here you need to convert the txtPurchasePrice to a floating point
>
> numeric before
>
> // you can start your calculations. Perhaps like the following:
>
>
>
> purchasePrice =
>
> parseFloat(document.getElementById("txtPurchasePrice").value) ;
>
>
>
> if (purchasePrice <= 25)
>
> shippingAndHandling = 1.50;
>
> else
>
> shippingAndHandling = purchasePrice * .10;
>
> totalCost = purchasePrice + shippingAndHandling;
>
>
>
> // now you should format your totalCost to an appropriate number of
>
> digits below
>
>
>
> window.alert('Purchase price plus shipping and handling is $' + totalCost);
>
> }
>
> </script>
>
> </head>
>
>
>
> <body>
>
> Purchase price (no dollar signs):
>
> <form id="form1" method="post" action="">
>
> <p>
>
> <label for="txtPurchasePrice"></label>
>
> <input type="text" name="txtPurchasePrice" id="txtPurchasePrice" />
>
> <input type="submit" name="btnCalculate" id="btnCalculate"
>
> value="Calculate" onclick="calculateShipping()" />
>
> </p>
>
> </form>
>
> </body>
>
> </html>
>
>
>
> For the record, I've also tried changing "txtPurchasePrice.value" to
>
> "document.getElementById('txtPurchasePrice').value" and even
>
> "txtPurchasePrice.text", but none of what I'm doing appears to make any
>
> difference -- no matter which way I go, it still says NaN.
Sadly, I think I tried that, and it also failed.
[toc] | [prev] | [next] | [standalone]
| From | "Mel Smith" <med_cutout_syntel@aol.com> |
|---|---|
| Date | 2012-11-11 10:56 -0700 |
| Message-ID | <aga76fF248tU1@mid.individual.net> |
| In reply to | #17169 |
<dmn2004404@gmail.com> wrote in message
news:f361ca82-911a-4f52-9132-3f0af5baf3ca@googlegroups.com...
> On Sunday, November 11, 2012 11:08:42 AM UTC-6, Mel Smith wrote:
>> where I'm going wrong?
> Sadly, I think I tried that, and it also failed.
Hi:
Well, *I* tested it on my machine, after inputting 123.45, and got a
nice long floating point result !!
Remember the *value* of an input field is *text* -- and is *not* a
numeric !
You must get this 'value' and convert it to a numeric by some means.
For example use parseFloat("123.45",10) . This says: Take the string
"123.45" and convert it to a floating point numeric with a base 10 ---
which is most surely what you wish.
So, take another 'head-crashing' look at it.
-Mel Smith
Mesa, Arizona
[toc] | [prev] | [next] | [standalone]
| From | dmn2004404@gmail.com |
|---|---|
| Date | 2012-11-11 10:07 -0800 |
| Message-ID | <06fc8ff0-b4fd-4518-9247-1a62fb158439@googlegroups.com> |
| In reply to | #17170 |
On Sunday, November 11, 2012 11:56:33 AM UTC-6, Mel Smith wrote:
> <dmn2004404@gmail.com> wrote in message
>
> news:f361ca82-911a-4f52-9132-3f0af5baf3ca@googlegroups.com...
>
> > On Sunday, November 11, 2012 11:08:42 AM UTC-6, Mel Smith wrote:
>
> >> where I'm going wrong?
>
>
>
> > Sadly, I think I tried that, and it also failed.
>
>
>
>
>
> Hi:
>
>
>
> Well, *I* tested it on my machine, after inputting 123.45, and got a
>
> nice long floating point result !!
>
>
>
> Remember the *value* of an input field is *text* -- and is *not* a
>
> numeric !
>
>
>
> You must get this 'value' and convert it to a numeric by some means.
>
> For example use parseFloat("123.45",10) . This says: Take the string
>
> "123.45" and convert it to a floating point numeric with a base 10 ---
>
> which is most surely what you wish.
>
>
>
> So, take another 'head-crashing' look at it.
>
>
>
> -Mel Smith
>
> Mesa, Arizona
GAAH! It's STILL not working!!!
[toc] | [prev] | [next] | [standalone]
| From | dmn2004404@gmail.com |
|---|---|
| Date | 2012-11-11 10:52 -0800 |
| Message-ID | <f4fcb90e-7930-4622-a518-472813d85cab@googlegroups.com> |
| In reply to | #17170 |
On Sunday, November 11, 2012 11:56:33 AM UTC-6, Mel Smith wrote:
> <dmn2004404@gmail.com> wrote in message
>
> news:f361ca82-911a-4f52-9132-3f0af5baf3ca@googlegroups.com...
>
> > On Sunday, November 11, 2012 11:08:42 AM UTC-6, Mel Smith wrote:
>
> >> where I'm going wrong?
>
>
>
> > Sadly, I think I tried that, and it also failed.
>
>
>
>
>
> Hi:
>
>
>
> Well, *I* tested it on my machine, after inputting 123.45, and got a
>
> nice long floating point result !!
>
>
>
> Remember the *value* of an input field is *text* -- and is *not* a
>
> numeric !
>
>
>
> You must get this 'value' and convert it to a numeric by some means.
>
> For example use parseFloat("123.45",10) . This says: Take the string
>
> "123.45" and convert it to a floating point numeric with a base 10 ---
>
> which is most surely what you wish.
>
>
>
> So, take another 'head-crashing' look at it.
>
>
>
> -Mel Smith
>
> Mesa, Arizona
Wait -- WAIT! I think I got it -- almost. I've had to make a few changes, as follows. Here's my revised JavaScript:
<script type="text/javascript">
var shipping, total;
function calculateShipping(purchasePrice) {
if (purchasePrice <= 25)
shipping = 1.50;
else
shipping = purchasePrice * .10;
total = purchasePrice + shipping;
window.alert('Purchase price plus shipping and handling is $' + total);
}
</script>
and the revised input tag for the button:
<input type="submit" name="btnCalculate" id="btnCalculate" value="Calculate" onclick="calculateShipping(parseFloat(txtPurchasePrice.value,10))" />
I believe the key was actually to put the value of the text box INSIDE the calculateShipping() function as a parameter.
Anyway, with these revisions, I tried the 123.45 value you attempted, and I got the LONG floating value of 135.79500000000002 -- which looked a little messy. So I attached the "toFixed(2)" method to "totalCost" at the end of the "window.alert" command, and it worked.
But now, I'd like to validate user box input so that if the user should enter a zero, a negative number, or something that actually IS "NaN" (e.g. "Hello world!"), the page will produce a dialog box that says the value is invalid. How might I go about doing that?
[toc] | [prev] | [next] | [standalone]
| From | JJ <jaejunks@glegooilma-swapit.com> |
|---|---|
| Date | 2012-11-11 20:15 +0000 |
| Message-ID | <XnsA10921EAC6664jaejunksglegooilma@0.0.0.9> |
| In reply to | #17174 |
dmn2004404@gmail.com wrote:
> Wait -- WAIT! I think I got it -- almost. I've had to make a few
> changes, as follows. Here's my revised JavaScript:
>
> <script type="text/javascript">
> var shipping, total;
> function calculateShipping(purchasePrice) {
> if (purchasePrice <= 25)
> shipping = 1.50;
> else
> shipping = purchasePrice * .10;
> total = purchasePrice + shipping;
> window.alert('Purchase price plus shipping and handling is $' +
> total);
> }
> </script>
>
> and the revised input tag for the button:
>
> <input type="submit" name="btnCalculate" id="btnCalculate"
> value="Calculate"
> onclick="calculateShipping(parseFloat(txtPurchasePrice.value,10))" />
>
> I believe the key was actually to put the value of the text box INSIDE
> the calculateShipping() function as a parameter.
>
> Anyway, with these revisions, I tried the 123.45 value you attempted,
> and I got the LONG floating value of 135.79500000000002 -- which
> looked a little messy. So I attached the "toFixed(2)" method to
> "totalCost" at the end of the "window.alert" command, and it worked.
>
> But now, I'd like to validate user box input so that if the user
> should enter a zero, a negative number, or something that actually IS
> "NaN" (e.g. "Hello world!"), the page will produce a dialog box that
> says the value is invalid. How might I go about doing that?
Make the calculateShipping function to return a boolean value. Returns
true to allow submit or false to disallow it. i.e.: due to invalid form
input(s).
Also check for NaN result each and every parseFloat using isNaN.
[toc] | [prev] | [next] | [standalone]
| From | dmn2004404@gmail.com |
|---|---|
| Date | 2012-11-11 12:45 -0800 |
| Message-ID | <465c0f13-6b2e-4802-9262-cf38bb8a1195@googlegroups.com> |
| In reply to | #17178 |
On Sunday, November 11, 2012 2:21:51 PM UTC-6, JJ wrote:
> dmn2004404@gmail.com wrote:
>
> > Wait -- WAIT! I think I got it -- almost. I've had to make a few
>
> > changes, as follows. Here's my revised JavaScript:
>
> >
>
> > <script type="text/javascript">
>
> > var shipping, total;
>
> > function calculateShipping(purchasePrice) {
>
> > if (purchasePrice <= 25)
>
> > shipping = 1.50;
>
> > else
>
> > shipping = purchasePrice * .10;
>
> > total = purchasePrice + shipping;
>
> > window.alert('Purchase price plus shipping and handling is $' +
>
> > total);
>
> > }
>
> > </script>
>
> >
>
> > and the revised input tag for the button:
>
> >
>
> > <input type="submit" name="btnCalculate" id="btnCalculate"
>
> > value="Calculate"
>
> > onclick="calculateShipping(parseFloat(txtPurchasePrice.value,10))" />
>
> >
>
> > I believe the key was actually to put the value of the text box INSIDE
>
> > the calculateShipping() function as a parameter.
>
> >
>
> > Anyway, with these revisions, I tried the 123.45 value you attempted,
>
> > and I got the LONG floating value of 135.79500000000002 -- which
>
> > looked a little messy. So I attached the "toFixed(2)" method to
>
> > "totalCost" at the end of the "window.alert" command, and it worked.
>
> >
>
> > But now, I'd like to validate user box input so that if the user
>
> > should enter a zero, a negative number, or something that actually IS
>
> > "NaN" (e.g. "Hello world!"), the page will produce a dialog box that
>
> > says the value is invalid. How might I go about doing that?
>
>
>
> Make the calculateShipping function to return a boolean value. Returns
>
> true to allow submit or false to disallow it. i.e.: due to invalid form
>
> input(s).
>
>
>
> Also check for NaN result each and every parseFloat using isNaN.
I think I finally got it! I added an extra if...else statement to make it complete. Here's my final script:
<script type="text/javascript">
var shipping, total;
function calculateShipping(purchasePrice) {
if (purchasePrice > 0) {
if (purchasePrice > 25)
shipping = purchasePrice * .10;
else
shipping = 1.50;
total = purchasePrice + shipping;
window.alert('Purchase price plus shipping and handling is $' + total);
} else
window.alert('Invalid value for purchase price! Please try again.');
}
</script>
Thanks guys, for all your help -- and to think I was on the verge of throwing in the towel on this one!
[toc] | [prev] | [next] | [standalone]
| From | JJ <jaejunks@glegooilma-swapit.com> |
|---|---|
| Date | 2012-11-11 20:11 +0000 |
| Message-ID | <XnsA109212F3FF24jaejunksglegooilma@0.0.0.9> |
| In reply to | #17174 |
dmn2004404@gmail.com wrote:
> Wait -- WAIT! I think I got it -- almost. I've had to make a few
> changes, as follows. Here's my revised JavaScript:
>
> <script type="text/javascript">
> var shipping, total;
> function calculateShipping(purchasePrice) {
> if (purchasePrice <= 25)
> shipping = 1.50;
> else
> shipping = purchasePrice * .10;
> total = purchasePrice + shipping;
> window.alert('Purchase price plus shipping and handling is $' +
> total);
> }
> </script>
>
> and the revised input tag for the button:
>
> <input type="submit" name="btnCalculate" id="btnCalculate"
> value="Calculate"
> onclick="calculateShipping(parseFloat(txtPurchasePrice.value,10))" />
>
> I believe the key was actually to put the value of the text box INSIDE
> the calculateShipping() function as a parameter.
>
> Anyway, with these revisions, I tried the 123.45 value you attempted,
> and I got the LONG floating value of 135.79500000000002 -- which
> looked a little messy. So I attached the "toFixed(2)" method to
> "totalCost" at the end of the "window.alert" command, and it worked.
>
> But now, I'd like to validate user box input so that if the user
> should enter a zero, a negative number, or something that actually IS
> "NaN" (e.g. "Hello world!"), the page will produce a dialog box that
> says the value is invalid. How might I go about doing that?
Make the calculateShipping function to return a boolean value. Returns
true to allow submit or false to disallow it. i.e.: due to invalid form
input(s).
Also check for NaN result each and every parseFloat using isNaN.
[toc] | [prev] | [next] | [standalone]
| From | dmn2004404@gmail.com |
|---|---|
| Date | 2012-11-11 12:15 -0800 |
| Message-ID | <50f3ae57-374c-4676-958e-9840baa4cf67@googlegroups.com> |
| In reply to | #17170 |
On Sunday, November 11, 2012 11:56:33 AM UTC-6, Mel Smith wrote:
> <dmn2004404@gmail.com> wrote in message
>
> news:f361ca82-911a-4f52-9132-3f0af5baf3ca@googlegroups.com...
>
> > On Sunday, November 11, 2012 11:08:42 AM UTC-6, Mel Smith wrote:
>
> >> where I'm going wrong?
>
>
>
> > Sadly, I think I tried that, and it also failed.
>
>
>
>
>
> Hi:
>
>
>
> Well, *I* tested it on my machine, after inputting 123.45, and got a
>
> nice long floating point result !!
>
>
>
> Remember the *value* of an input field is *text* -- and is *not* a
>
> numeric !
>
>
>
> You must get this 'value' and convert it to a numeric by some means.
>
> For example use parseFloat("123.45",10) . This says: Take the string
>
> "123.45" and convert it to a floating point numeric with a base 10 ---
>
> which is most surely what you wish.
>
>
>
> So, take another 'head-crashing' look at it.
>
>
>
> -Mel Smith
>
> Mesa, Arizona
Wait -- WAIT! I think I got it -- almost. I've had to make a few changes, as follows. Here's my revised JavaScript:
<script type="text/javascript">
var shipping, total;
function calculateShipping(purchasePrice) {
if (purchasePrice <= 25)
shipping = 1.50;
else
shipping = purchasePrice * .10;
total = purchasePrice + shipping;
window.alert('Purchase price plus shipping and handling is $' + total);
}
</script>
and the revised input tag for the button:
<input type="submit" name="btnCalculate" id="btnCalculate" value="Calculate" onclick="calculateShipping(parseFloat(txtPurchasePrice.value,10))" />
I believe the key was actually to put the value of the text box INSIDE the calculateShipping() function as a parameter.
Anyway, with these revisions, I tried the 123.45 value you attempted, and I got the LONG floating value of 135.79500000000002 -- which looked a little messy. So I attached the "toFixed(2)" method to "totalCost" at the end of the "window.alert" command, and it worked.
NOW, for the final touch: I'm trying to validate this document according to the XHTML 1.0 Strict DTD (http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd), and, after another modification in the JavaScript, I'm running into a problem, thus:
Validation Output: 1 Error
Line 24, Column 41: document type does not allow element "form" here; missing one of "object", "ins", "del", "map" start-tag
<form action="" method="post" id="form1">✉
The mentioned element is not allowed to appear in the context in which you've placed it; the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. This might mean that you need a containing element, or possibly that you've forgotten to close a previous element.
One possible cause for this message is that you have attempted to put a block-level element (such as "<p>" or "<table>") inside an inline element (such as "<a>", "<span>", or "<font>").
What should be my final step to alleviate this problem?
[toc] | [prev] | [next] | [standalone]
| From | Osmo Saarikumpu <osmo@weppipakki.com> |
|---|---|
| Date | 2012-11-12 10:10 +0200 |
| Message-ID | <k7qat7$tu$1@speranza.aioe.org> |
| In reply to | #17177 |
On 11.11.2012 22:15, dmn2004404@gmail.com wrote: > Validation Output: 1 Error [...] > What should be my final step to alleviate this problem? Present the question and the _entire document_ (preferably a URL) in the proper newsgroup: comp.infosystems.www.authoring.html -- Best wishes, Osmo
[toc] | [prev] | [next] | [standalone]
| From | Gregor Kofler <usenet@gregorkofler.com> |
|---|---|
| Date | 2012-11-12 09:51 +0100 |
| Message-ID | <k7qdb9$a45$1@dont-email.me> |
| In reply to | #17166 |
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
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-11-12 11:49 +0100 |
| Message-ID | <1660368.mReO0LyTN9@PointedEars.de> |
| In reply to | #17187 |
Gregor Kofler wrote:
> Am 2012-11-11 17:28, dmn2004404@gmail.com meinte:
>
> [snip]
>
>> <head>
>> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
This line is meaningless in XHTML unless parsed as text/html. The XHTML
document has to be well-formed before this line is parsed.
>> <title>Untitled Document</title>
Pardon?
>> <script type="text/javascript">
>> var purchasePrice = txtPurchasePrice.value, shippingAndHandling,
>> totalCost;
Should be
var purchasePrice = txtPurchasePrice.value;
var shippingAndHandling, totalCost;
to keep the code readable to begin with.
> Should already throw an error (reference error), since txtPurchasePrice
> is undefined at this point and txtPurchasePrice.value can't be evaluated.
The correct explanation for a ReferenceError exception at this line would be
that no object in the scope chain has a `txtPurchasePrice' property.
(Accessing a property of `undefined' would throw a TypeError exception
instead.)
The former would be the case here because the element named
`txtPurchasePrice' further below in this HTML document, which could create a
corresponding property on the Global or window objects, both of which are in
the scope chain in a browser context, the latter for backwards
compatibility, has not been parsed yet.
Such referencing is error-prone to begin with, of course. Form controls
should be referenced through the `elements' collection of an item of the
`forms' collection of object implementing the HTMLDocument interface, such
as that referred to by `document' or `window.document' in browsers …
>> function calculateShipping() {
>
> Something like
>
> var purchasePrice =
> parseFloat(document.forms["form1"].elements["txtPurchasePrice"].value)
>
> *in* the function should do the trick.
… as can be seen here.
> (Multiplying a "float-like" string with a float will also result in a
> float).
Whereas all Number values are floating-point values in these languages.
>> if (purchasePrice <= 25)
This is _not_ Valid XHTML because the content model of the XHTML `script'
element is PCDATA, and `<' is considered a markup special character (Start
TAG Open [STAGO] delimiter) there. Either mark up the content of the
`script' element as CDATA (using single-line comments to hide it from script
engines when the markup is not parsed as XHTML) –
<script type="text/javascript">
//<![CDATA[
//]]>
</script>
or do not declare, and do not use XHTML.
>> shippingAndHandling = 1.50;
>> else
>> shippingAndHandling = purchasePrice * .10;
>> totalCost = purchasePrice + shippingAndHandling;
>> window.alert('Purchase price plus shipping and handling is $' +
>> totalCost); }
window.alert() should not be used to convey purely informational messages.
>> </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.
And with unqualified references on which rendering mode is used.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web