user4913383
user4913383

Reputation: 171

Getting "Uncaught SyntaxError: Unexpected token ;" error while using for loop

Here I have a loop to iterator over html elements in a JSF application.

        var tmp = document.getElementsByTagName("input");
        var counter = 0;
        var arr = new Array();
        for (var i = 0; i<tmp.length; i++) {
            if (tmp[i].parentNode.nodeName === "TD" && tmp[i].getAttribute("type") !== "submit") {
                arr[counter] = tmp[i];
                counter++;
            }
        }

This loop is inside this script tag:

<script type="text/javascript">
<!--    
     //-->
</script>

When I load the page in the web browser I get following error(shown in google chrome developer tools)

enter image description here enter image description here

Upvotes: 0

Views: 779

Answers (2)

Michael Geary
Michael Geary

Reputation: 28870

As you can see in your screenshot, your script in the HTML page has &lt; where it should have <. This is your syntax error. Are you generating this page with some templating language that "escapes" HTML code? That would cause this problem.

One good solution is to put your JavaScript code in a separate .js file. That should avoid the escaping and leave your code unchanged.

If you do need to have inline JavaScript right there in the HTML page, most templating languages have a way to turn off escaping for a specific piece of text.

Upvotes: 3

Cu3PO42
Cu3PO42

Reputation: 1473

The line in picture 2 isn't the same as the one in picture 1. < was substituted for &lt;, which is the HTML escape code for <. The error subsequently does not lie within your code itself, but somewhere in your development workflow. You either saved the file with the wrong contents (which I presume to be unlikely) or your web server or any pre processor is messing up by HTML escaping code inside script-tags.

Upvotes: 2

Related Questions