Search code examples
javascripthtmlxmlhttprequest

Uncaught TypeError: translate is not a function


Keep getting an uncaught type error when I click the button that is supposed to trigger the translate function. Unsure what is causing this. I've tried changing the element from a button element to an input element with type button. Whatever I do I still get this error. Anyone able to help?

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="Content-Language" content="en-us">
    <title>Example Web Editor</title>
    <link rel="stylesheet" type="text/css" href="xtext/2.24.0/xtext-ace.css"/>
    <link rel="stylesheet" type="text/css" href="style.css"/>
    <script src="webjars/requirejs/2.3.6/require.min.js"></script>
    <script type="text/javascript">
        var baseUrl = window.location.pathname;
        var fileIndex = baseUrl.indexOf("index.html");
        if (fileIndex > 0)
            baseUrl = baseUrl.slice(0, fileIndex);
        require.config({
            baseUrl: baseUrl,
            paths: {
                "jquery": "webjars/jquery/3.5.1/jquery.min",
                "ace/ext/language_tools": "webjars/ace/1.3.3/src/ext-language_tools",
                "xtext/xtext-ace": "xtext/2.24.0/xtext-ace"
            }
        });
        require(["webjars/ace/1.3.3/src/ace"], function() {
            require(["xtext/xtext-ace"], function(xtext) {
                xtext.createEditor({
                    baseUrl: baseUrl,
                    syntaxDefinition: "xtext-resources/generated/mode-logic"
                });
            });
        });
    </script>
    
    <script>
        function translate() {
            console.log("function being triggered.");
            let req = new XMLHttpRequest();
            console.log("request created");
            req.open( "POST", "localhost:8003/translate", false);
            req.send(ace.edit('xtext-editor').getSession().getValue());
            return req.responseText;
            }
    </script>
</head>

<div class="container">
    <div class="header">
        <h1>Example LogicLang Web Editor</h1>
    </div>
    <div class="content">
        <div id="xtext-editor" data-editor-xtext-lang="logic"></div>
    </div>
</div>
<form onsubmit = "return false">
<input type="button" onclick="translate()"> Compile </input>
<button class="save-button" onclick="console.log(ace.edit('xtext-editor').getSession().getValue());"> Test </button>
</form>
</body>
</html>


Solution

  • You need to use a different name for that function.
    translate() is already an existing method used for 2D Canvas drawings.

    The translate() is a method of a 2D drawing context. The translate(x,y) method moves the canvas and its origin x units horizontally and y units vertically. source.