Search code examples
javascriptjqueryhtmljquery-after

jQuery after() Uncaught SyntaxError: Unexpected token ILLEGAL


for (var i = 0; i < rows.length; i++) {   
      $('#rows').after('<tr>
                           <td>'+ i + 1 + '</td>'
                           <td>'+rows[i][\'title\']+'</td>
                           <td>'+rows[i][\'startTime\']+' 
                        </tr>'); 
}

In the above jquery code, I would like to insert the table rows after the div with "rows" class in my html. However, I'm getting the error

Uncaught SyntaxError: Unexpected token ILLEGAL

may I know what is wrong and how can I solve it? thanks


Solution

  • You cannot just put a new line character in the middle of the string in js, you must put a slash in the end of every line:

    var a = 'foo \
    bar \
    baz';
    

    alternatively you can concatenate several strings, one line each:

    var a = 'foo ' +
            'bar ' +
            'baz';
    

    Both examples are equivalent to:

    var a = 'foo bar baz';
    

    Addiitonally, rows[i][\'title\'] is not a correct syntax either - you must not put slashes there.