0

I need to ensure the minimum answer is 5 and the maximum can be no larger than 25. The code I use is:

function temp(form)
{
    var i = parseFloat(form.Inc.value, 10);
    var c = 0;
    c = 25 - ((300 - i) * 8.0 / 100);
   form.Fee.value = c.toFixed(2);

   function decrease(form)
   {
        if (c > 25) 
        {
           c--;
           document.getElementById('Fee').innerHTML = 25;
        }
   }

   function increase(form)
   {
       if (c < 5) 
       {
           c++;
           document.getElementById('Fee').innerHTML = 5;
       }
   }
}

However the answer box in the form doesn't recognise the minimum and maximum figures.

Cœur
  • 32,421
  • 21
  • 173
  • 232
GregSmith
  • 3
  • 1
  • You should add some more explanation. As it stands I have almost no clue, what this code is intended to do. – Sirko Aug 28 '13 at 11:35
  • Sorry, I didn't realise. – GregSmith Aug 28 '13 at 11:39
  • @Sirko The code enables a person to put a value (i) into a form which then calculates the answer. the fo=rmula for the equation is:The value (i) is a weekly income amount and the answer is the fee (c) charged. – GregSmith Aug 28 '13 at 11:42
  • The formula for the answer is:c = 25 - ((300 - i) * 8.0 / 100) – GregSmith Aug 28 '13 at 11:42

1 Answers1

0

form.Fee.value implies that your form has an element named "Fee".
The "name" attribute is different from the "id" attribute, and as you've likely not given your form elements 'id's, document.getElementById() is presumably returning null.
Consider using document.getElementsByName or using form.Fee.

Additionally, the decrease() and increase() methods aren't being invoked.
I think you're looking for code like this:

 function temp(form)
 {
    var i = parseFloat(form.Inc.value, 10);
    var c = 0;
    c = 25 - ((300 - i) * 8.0 / 100);

    // Alternatively use Math.min and Math.max
    if ( c > 25 ) c = 25; 
    if ( c < 5 ) c = 5;

    form.Fee.value = c.toFixed(2);
 }

Additional notes; var c can be initialized with the expression in the line right below it; the initialization to 0 is unnecessary.

Community
  • 1
  • 1
Warty
  • 6,709
  • 1
  • 26
  • 47