MrPaulius
MrPaulius

Reputation: 107

javascript error: function is not defined

I was trying to find the reason why my code doesnt work, and chrome comes back with this eror: Uncaught ReferenceError: calcMPG is not defined... Can someone spot my mistake ?(desperate)

<script type="text/javascript">
    function calcMPG() {
        document.calc.startingMileage.value = startMileage;
        document.calc.endingMileage.value = endMileage;
        document.calc.gallonsUsed.value = gallUsed;
        var MPG = (endMileage - startMileage) / gallUsed;
        if (isNAN(startMileage) || isNAN(endMileage) || isNAN(gallUsed)) alert('please type in numbers only!');
        else document.calc.milesPerGalon.value = MPG;
    }
</script>

<form name="calc">Starting mileage:
    <input type="text" value="0" name="startingMileage" onchange="calcMPG()">
    <br>Ending mileage:
    <input type="text" value="0" name="endingMileage" onchange="calcMPG()">
    <br>Gallons used:
    <input type="text" value="0" name="gallonsUsed" onchange="calcMPG()">
    <br>Miles per galon:
    <input type="text" value="0" name="milesPerGalon">
</form>

Upvotes: 3

Views: 46711

Answers (4)

Jonast92
Jonast92

Reputation: 4967

startMileage

and your other right side references don't mean anything, they are undefined variables.

Give your inputs an id and fetch the values from them like this:

var startingMileage = document.getElementById('startingMileage').value;

Upvotes: 1

Dmitry
Dmitry

Reputation: 867

Check here Fiddle

 function calcMPG(){
        var startMileage = document.calc.startingMileage.value;
        var endMileage = document.calc.endingMileage.value;
        var gallUsed = document.calc.gallonsUsed.value;

        var MPG = (endMileage - startMileage) / gallUsed; 

        if(isNaN(startMileage) || isNaN(endMileage) || isNaN(gallUsed))
            alert('please type in numbers only!');
        else 
            document.calc.milesPerGalon.value = MPG;
    }

Upvotes: 0

Satyam Koyani
Satyam Koyani

Reputation: 4274

Your declarations are wrong please correct them.

var startMileage = document.calc.startingMileage.value ;
var endMileage =  document.calc.endingMileage.value;
var gallUsed =  document.calc.gallonsUsed.value;

Upvotes: 6

Marco God&#237;nez
Marco God&#237;nez

Reputation: 3470

I don't know why you do that, but try this:

<script type="text/javascript">
  function calcMPG(){
    var startMileage  = document.calc.startingMileage.value,
        endMileage    = document.calc.endingMileage.value,
        gallUsed      = document.calc.gallonsUsed.value,
        MPG           = (endMileage - startMileage) / gallUsed;

      if(isNaN(startMileage) || isNaN(endMileage) || isNaN(gallUsed)){
        alert('please type in numbers only!');
      } else {
        document.calc.milesPerGalon.value = MPG;
      }
    }
</script>
<form name="calc">
    Starting mileage:<input type="text" value="0" name="startingMileage" onchange="calcMPG()"><br>
    Ending mileage:<input type="text" value="0" name="endingMileage" onchange="calcMPG()"><br>
    Gallons used:<input type="text" value="0" name="gallonsUsed" onchange="calcMPG()"><br>
    Miles per galon:<input type="text" value="0" name="milesPerGalon">
</form>

Upvotes: 1

Related Questions