0

I am trying to call a function in an external JavaScript file from an HTML-file. The goal is to work with the content of a form there.

I tried so many things and always got the Error "ReferenceError: Can't find variable: X"

  • The positioning of the jquery javascript file loading
  • The positioning of the <script src="XXX"> call
  • Calling the function from the button "onclick" or the form "onsubmit"
  • Trying to call the javascript file from an embedded script in the HTML

This is what my JavaScript file and my HTML looks like right now:

function submit(e) {

  answerText = document.getElementById("text").value;
  // Do something with it.

}
<head>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
  <script src="/scripts/debug_alerts.js" type="text/javascript"></script>
</head>
<body>
  <form id="validation-form" onsubmit="return submit(e)">
    <input type="text" class="form-control" name="text" id="text" placeholder="Text" required>
    <button type="button" class="btn btn-primary" onclick="submit(e)">Send</button>
  </form>
</body>

I also tried

function doSomething() {

  // Do something

}
<head>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
  <script src="/scripts/debug_alerts.js" type="text/javascript"></script>
</head>
<body>
  <form id="validation-form" onsubmit="return submit(e)">
    <input type="text" class="form-control" name="text" id="text" placeholder="Text" required>
    <button type="button" class="btn btn-primary" onclick="submit(e)">Send</button>
  </form>
  <script>
    function submit(e) {
      doSomething();
    }
  </script>
</body>

In both cases, it returned the same error over and over again: "ReferenceError: Can't find variable: X". In the first example, X being "submit" and in the second "doSomething".

All help is very welcome. I know there are similar headlines here, but non of the solutions did anything for me.

5
  • Is the error message you get ReferenceError: Can't find variable: $ or ReferenceError: Can't find variable: submit? Commented May 11, 2020 at 17:11
  • The error you get in your examples is different, and relates to e being not defined. Commented May 11, 2020 at 17:13
  • 1
    I am getting a different error here like Uncaught ReferenceError: e is not defined Commented May 11, 2020 at 17:19
  • Can you provide the exact error message please? Commented May 11, 2020 at 17:22
  • Looks like you just need to change onclick="submit(e)" to onclick="submit(this)" or even just onclick="submit()" if you don't need the event - or use jquery events instead of inline ones. Commented May 11, 2020 at 17:24

1 Answer 1

1

Hey so when I ran the code without the e as a parameter for the submit function in the html it didn't give me the error. I think it may be because the e is the place holder for the text of the submit function in this case. Hope this helps.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <script src="/scripts/debug_alerts.js" type="text/javascript"></script>
</head>

<body>
    <form id="validation-form" onsubmit="return submit(e)">
        <input type="text" class="form-control" name="text" id="text" placeholder="Text" required>
        <button type="button" class="btn btn-primary" onclick="submit()">Send</button>
    </form>
    <script type="text/javascript">
        function submit(e) {

            answerText = document.getElementById("text").value;
            // Do something with it.


        }
    </script>
</body>

</html>
Sign up to request clarification or add additional context in comments.

Comments

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.