0

I know there are a lot of questions like this, but none of them seemed to solve my problem. I have this piece of code that won't run because it says Uncaught ReferenceError: run is not defined. I have tried to move the function into the body of the HTML, but to no avail. My code:

<!DOCTYPE html>
<html>
    <textarea name="Text1" cols="100"rows="20" id="textbox">
    </textarea>
    <button onclick="run()">Export to C++</button>
    <script type="text/javascript">
        function run() {
            var code=new Array();
            var input = document.getElementById("textbox").value;
            //convert things that are not subroutines here
            code.push(input);
            code.push("}");
        ...
        for (var i=0;i<code.length;i++)
            {
                document.write(code[i]+"<br>");
            }
        }
    </script>
</html>

The ... is irrelevant code.

Why isn't this working? Any ideas on how to fix it?

Thanks

Turnip
  • 35,836
  • 15
  • 89
  • 111
APCoding
  • 303
  • 2
  • 19

1 Answers1

1

Seems it working fine for me, but as I can see the only reason for the problem is the following.

Your page is loading piece by piece from up to down, so all the scripts are going to be included and executed one by one, all the elements are going to be shown one by one as well.

That's not this case in fact, because you are using "on click" event and there are no init actions, so it should be working, but you can try to move your <script></script> at the top (before you assign event).

<!DOCTYPE html>
<html>
    <textarea name="Text1" cols="100"rows="20" id="textbox">
    </textarea>
    <script type="text/javascript">
         you script here
    </script>
    <button onclick="run()">Export to C++</button>

</html>

You may also replace the whole code inside of <script></script> by something like alert("Hello"); to check if it's working. Possible you have the issue with internal code.

Roman Sachenko
  • 558
  • 3
  • 8