Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #17166
| Newsgroups | comp.lang.javascript |
|---|---|
| Date | 2012-11-11 08:28 -0800 |
| Message-ID | <055e7215-93ba-40a9-81f3-312ea0e4f5e2@googlegroups.com> (permalink) |
| Subject | JavaScript Case Project 3-1: The dreaded NaN error |
| From | dmn2004404@gmail.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.
Back to comp.lang.javascript | Previous | Next — Next in thread | Find similar | Unroll 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