0

ReferenceError allthough function is defined.

tried renaming

<body>
   <script>
    function yaSuggestion() {
      console.log("hello");
      var input = document.getElementById("yaIsInput");
      var request = new XMLHttpRequest();

      request.open("GET", "http://localhost:3000/issues/get");
      request.addEventListener('load', function (event) {
        if (request.status >= 200 && request.status < 300) {
          //console.log(request.responseText);
        } else {
          //console.warn(request.statusText, request.responseText);
        }
      });
      request.send();
      request.onreadystatechange = processRequest();
      function processRequest() {
        if (request.readyState == 4 && request.status == 200) {
          console.log(request.responseText);
          var res = JSON.parse(request.responseText);
          var suggestions = [];
          for (int i = 0; i < res.length; i++) {
            suggestions.push(res[i].value);
          };
          console.log(suggestions);
        }
      }
    }
  </script>
<input type="text" name="yaIsInput" class="form-control" id="yaIsInput" onchange="yaSuggestion()">
</body>

expected: console output "hello" actual: ReferenceError: yaSuggestion is not defined[Learn More] ya_create:1:1

6
  • 3
    Should be closed because the problem is caused by a typo. Deal with the first error first. You have a stray } that prevents the function from being defined. Commented Apr 18, 2019 at 11:43
  • The typo was limited to the question version, which is now updated. Error persists. Commented Apr 18, 2019 at 13:10
  • The error does not persist. The live demo (after your edit) in the question works: i.imgur.com/2sloLBc.png Commented Apr 18, 2019 at 13:11
  • True. Updated the code snippet to show my actual code. If I run it like this, then the error might occur. Hope you can help here as well. Commented Apr 18, 2019 at 13:30
  • Typo: processRequest(); has an extra () so it calls the function imediately (before the readyState has changed) so the condition request.readyState == 4 && request.status == 200 is never true, so it does nothing. You should be using the load event handler instead. Commented Apr 18, 2019 at 13:35

1 Answer 1

1

Hi first thing you have an additional closing '}' in your script :

use this

 function yaSuggestion() {
          console.log("hello");
          }

second you have to import your script before calling the function :

   <body>
      <script>
        function yaSuggestion() {
          console.log("hello");
          }
      </script>
      <input type="text" name="yaIsInput" class="form-control" id="yaIsInput" onchange="yaSuggestion()">
    </body>

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

2 Comments

The order the code appears in does not matter in this case.
@Quentin thank you for pointing that out, I agree with you it won't matter in this case, it is just a good practice to have the script imported before it is used.

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.