3

Here is my code, I don't understand what's wrong.

    <script type="text/jquery">

function gettotalAdult()
{
    //Assume form with id="fsd-bucket-calc"
    var theForm = document.forms["fsd-bucket-calc"];
    //Get a reference to the # of Adults & Children
    var quantity = theForm.elements["totalAdult"];
    var caloriesAdult = theForm.elements["caloriesAdult"];
    var adultcalTotal=0;
    //If the totalAdult is not blank
    if(totalAdult.value!="")
    {
        adultcalTotal = parseInt(totalAdult.value)*parseInt(caloriesAdult.value);
    }
return adultcalTotal;
}

function gettotalChild()
{
    //Assume form with id="fsd-bucket-calc"
    var theForm = document.forms["fsd-bucket-calc"];
    //Get a reference to the # of Children
    var totalChild = theForm.elements["totalChild"];
    var caloriesChild = theForm.elements["caloriesChild"];
    var childcalTotal=0;
    //If the totalChild is not blank
    if(totalChild.value!="")
    {
        childcalTotal = parseInt(totalChild.value)*parseInt(caloriesChild.value);
    }
return childcalTotal;
}

function gettotalCalories()
{
    //Here we get the total calories by calling our function
    //Each function returns a number so by calling them we add the values they return together
    var totalCalories = gettotalAdult() + gettotalChild();
    //display the result
    document.getElementById('total-req-cal').innerHTML = "The total required calories are "+totalCalories;
}

</script>

This is my HTML:

<input type="text" name="totalAdult" id="totalAdult" onkeyup="gettotalCalories()" />

This is my error:

gettotalCalories is not defined

If it helps, the script is in the head of a WordPress page. Does anyone see what I'm doing wrong?

7
  • This may be a sneaky error - see stackoverflow.com/questions/153909/… Oct 23, 2011 at 0:14
  • @Adel - Your link is a circular reference. Oct 23, 2011 at 0:14
  • I'm not seeing anything specific wrong with what you posted: jsfiddle.net/LZVp6 Oct 23, 2011 at 0:16
  • Are you sure your script is loading into the page. Have you fired up firebug or chrome's console to see what scripts are loaded? Oct 23, 2011 at 0:18
  • $this->gettotalAdult() I don't think you can use -> syntax in JS, can you?
    – vol7ron
    Oct 23, 2011 at 0:21

1 Answer 1

6

You have <script type="text/jquery"> you may need <script type="text/javascript"> instead.

5
  • lol... I think that was it! Now I'm getting a different error: Cannot set property 'innerHTML' of null, but at least I can start tracing that down now that something is working! Oct 23, 2011 at 0:28
  • Just saw this, you beat me to it.
    – vol7ron
    Oct 23, 2011 at 0:28
  • When I first noticed that, I didn't think that was so important. Oct 23, 2011 at 0:32
  • @MichaelDavis you shouldn't change the question... it makes the answer more confusing
    – vol7ron
    Oct 23, 2011 at 0:38
  • @vol7ron - Good point! I'll edit that... I appreciate all the help! I'm always amazed how responsive everyone is here. Oct 23, 2011 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.