0

I have a piece of JavaScript code that keeps throwing an 'Is Undefined' error.

An undefined error from the inspect element function, showing an error that reads - 'checkScheduleAndAmount' is undefined - and highlights the onclick element - return checkScheduleAndAmount() - "

< script language = "javascript"
type = "text/javascript" >
  // fuction added for click on submit - dp0jmr 05/23/2018
  function checkScheduleAndAmount() {

    var ppAmt = (double)
    <%=p.getPaymentPlanAmt()%>;
    var totalamt = (double) document.getElementById("sum").innerText;

    if (ppAmt != totalamt) {
      alert("The Payment Plan Schedule does not add up to the total Payment Plan Amount - this Payment Plan cannot be submitted." +
        " Please correct the Amounts entered and submit the Payment Plan Schedule before leaving this page." +
        "\n\nIf the Date Range you have entered does not allow you to enter the Plan you desire, please End this Payment Plan " +
        "and begin a new one." +
        "\n\nIf you know the installment amount you wish to use, you can enter an installment amount at the start of a new " +
        "Payment Plan, and the application will calculate the final payoff date for you.  ");

      return false;
    } else {

      return true;
    }

  }

  <
  /script>

I've eliminated as many possible culprits as I could:

  • I've tried every combination of script definitions - there is some JQuery in addition to this JavaScript, but it was running fine alongside it until recently.
  • I tried putting it inside the page element with no effect.
  • I don't see any obvious syntax errors - all the variables being used here are defined.

The function call is on an html:button tag, at the very bottom of the div.

            <html:submit property="submitValue" value="<%=PaymentPlanDetailsForm.SUBMIT%>" styleClass="button" disabled="<%=isActive %>" onclick="return checkScheduleAndAmount()" onkeypress="return false"/>&nbsp;&nbsp;  

This started to occur recently after refining my JQuery function, but both functions seemed to be working fine during testing, and even seemed to work without issue together for awhile - and unfortunately, I cannot revert my changes now because I made the mistake of closing the IDE. :(

Am I missing something obvious in the syntax for this? Or is there another reason my page isn't recognizing my javascript function?

9
  • It would help a lot if you told us what it's saying is undefined, ideally by copying and pasting the error message and telling us what line of code it relates to. Commented May 30, 2018 at 15:26
  • @T.J.Crowder I have added an image to show the error - it's from inspect element. I've also done my best to describe it. The error is occuring on the "onclick" attribute of the Submit button "Submit", and says - 'checkScheduleAndAmount' is undefined -. Commented May 30, 2018 at 15:32
  • 1
    You probably have two errors first syntax error mention by @T.J.Crowder and second that function is missing because the script was broken. Commented May 30, 2018 at 15:33
  • @jcubic It's such a pain too - when part of the function is broken, the entire function breaks, but all I get for an error is that the function is 'not defined' - which is a vague and misguiding error message to get for a function that simply has a syntax error I missed. Commented May 30, 2018 at 15:38
  • What is double? Commented May 30, 2018 at 15:38

1 Answer 1

4

This JavaScript code:

var totalamt = (double) document.getElementById("sum").innerText;

...is invalid JavaScript code, and so the parsing fails, and the function isn't created.

JavaScript is not C# or Java or (insert language here). It doesn't have casting. Just remove the (double) part. If you want to convert that string to a number, use a unary +, the Number function, parseInt, or parseFloat.

For instance, if you want to convert all of the text to a number, and treat a blank as an invalid input, then:

var str = document.getElementById("sum").innerText;
var totalamt = str ? +str : NaN;
if (isNaN(totalamt)) {
    // ...it wasn't a valid number
}

As I mentioned, you could also use parseInt or parseFloat, but beware that they accept numbers with trailing non-numeric characters (parseFloat("123.4abc") is 123.4, for instance).

Sign up to request clarification or add additional context in comments.

2 Comments

So it was just a matter of trying to cast the variables as double being an invalid command for javascript...I wish the error that got thrown was more specific, but at least now I know. Thank you very much for the quick reply. This worked perfectly.
@Zibbobz: You should have seen a syntax error message in your browser's console.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.