clifgray
clifgray

Reputation: 4419

SyntaxError: Unexpected token ILLEGAL on Function Call

I know what this error is generally from but I just can't find it in my code. I know it is the statement:

button.setAttribute("onclick", "makeUneditable(" + row_id + ")")

And I've included all the code so you can know what you're looking at. And just fyi {{row}} is a list that I pass in with Jinja2 and then convert to a string in javascript. But I am not sure what in that statement is illegal. Any ideas?

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <script>
        function makeEditable(row_id) {
            row_id = String(row_id)
            var row = document.getElementById(row_id); 
            row.setAttribute("contenteditable", "true")

            var button = document.getElementById(row_id+"_button"); 
            button.setAttribute("onclick", "makeUneditable(" + row_id + ")")
            button.innerHTML = "Done Editing";
        }

        function makeUneditable(row_id) {
            row_id = String(row_id)
            console.log(row_id)
            var row = document.getElementById(row_id); 
            row.setAttribute("contenteditable", "false")
        }

    </script>

</head> 
<body>


    <table class="TFtable" style="width:300px">
        <thead>
        {% for column_name in column_names %}
        <th>{{column_name}}</th>
        {% endfor %}
        </thead>
        {% for row in table %}
            <script>
                document.writeln('<tr id="' + String({{row}}) + '">');
            </script>
                {% for cell in row %}
                <td>{{cell}}</td>
                {% endfor %}
                <td contenteditable="false">
                    <script>
                        document.writeln('<button id="' + String({{row}})+'_button"');
                    </script>
                    type="button" onclick="makeEditable({{row}})">Edit Connector</button>
                </td>
            </tr>           
        {% endfor %}
    </table>        
</body>

Upvotes: 0

Views: 112

Answers (1)

Muhammad Ali
Muhammad Ali

Reputation: 2014

Use single quotes around your value when concatenating it:

button.setAttribute("onclick", "makeUneditable('" + row_id + "')")

Upvotes: 1

Related Questions