0

I have a javascript function embeded in my html code but when i'm running the page i get this error: Uncaught ReferenceError: myFunction is not defined.

I'm also using angularjs, so i have my index page and, as they explain, my partial page, and that functioon is in my partial page.

When i double click the web page from the folder i can run the script, but when i run the web api it present that error.

My javascript function:

<script type="text/javascript">
      function myFunction() {
          var x;
          var r = confirm("Press a button!");
          if (r == true) {
              x = "You pressed OK!";
          }
          else {
              x = "You pressed Cancel!";
          }
          document.getElementById("demo").innerHTML = x;
      }

and how i call it:

<button onclick="myFunction()">Try it</button>

can anyone help me? thk

1

1 Answer 1

2

It seems it is trying to use the function even before it is loaded. Can you try below.

<script type="text/javascript">
      function myLoad() {
          var myButton = document.getElementById('myButton');

          myButton.addEventListener('click', function() {
             var x;
             var r = confirm("Press a button!");
             if (r == true) {
                x = "You pressed OK!";
             }
             else {
                x = "You pressed Cancel!";
             }

             document.getElementById("demo").innerHTML = x;
          }, false);
       }

       window.load = myLoad;
</script>

Finally add an Id to the button.

<button id="myButton">Try it</button>
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.