View Full Version : Please help - simple script that won't work!

12-21-2010, 08:38 AM
I am a very novice web coder and can't get this to work... I've tried everything and am getting no where but frustrated! Thanks in advance!!



<!-- Begin
function calculateSavings()
var lastBill = eval(document.theForm.elements[0].value);
var frequencyRating = eval(document.theForm.elements[1].value);
var savingsRate = .05;
var annualSavings = 0;

if (frequencyRating == 'every day)
savingsRate = .01;
else if (frequencyRating == 'weekly)
savingsRate = .05;
else if (frequencyRating == 'monthly')
savingsRate = .15;
else if (frequencyRating == 'yearly')
savingsRate = .2;

annualSavings = lastBill * savingsRate;

// End -->



<form name="savingsCalcForm">
How much was your last bill?
<br><INPUT TYPE="text">
How often are you using your tractor?
<br><input type="radio" name="group1" value="every day"> every day<br>
<input type="radio" name="group1" value="weekly "> weekly<br>
<input type="radio" name="group1" value="monthly"> monthly<br>
<input type="radio" name="group1" value="yearly"> yearly<br>
<INPUT TYPE="button" VALUE="calculate savings" onClick="calculateSavings();">




12-21-2010, 09:16 AM
I am a very novice web coder and can't get this to work...
This should do what? Please, tell us what should do that code, as well.

Philip M
12-21-2010, 09:32 AM
Homework? Have you tried using your error console? There are quite a few errors and you seem unaware of how to capture the value of a checked radio button.

<script type = "text/javascript">

function calculateSavings() {
var lastBill = document.savingsCalcForm.elements[0].value;
var len = document.savingsCalcForm.group1.length;
for (var i =0; i< len; i++){
if (document.savingsCalcForm.group1[i].checked) {
var frequencyRating = document.savingsCalcForm.group1[i].value;

Therre are other errors as well in the commented out section. If you had used " instead of ' they would have been more obvious.

a) Do not use eval!! Insecure and totally pointless here, but in any case mostly undesirable as there is almost always a better method.

b) Prefer to use the names of the form elements rather than elements[0] - you should assign a name to your textbox.

c) You need to validate the value of lastBill entered into the textbox to ensure that it is a positive number. Obviously you cannot multiply a string value.

d) <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead. The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

e) If you made the values of the radio buttons 0.01, 0.05 etc. then you would be able to take these directly as the value of frequencyRating.
annualSavings = lastBill * frequencyRating;

f) document.write statements must be run before the page finishes loading. You will have noticed that any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

BTW, the time to say "thanks" is afterwards, not beforehand which gives the - doubtless unintended - impression that you take other people's voluntary unpaid assistance and expertise for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.

"In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)