jQuery after() Uncaught SyntaxError: Unexpected token ILLEGAL

364 Views Asked by At
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

3

There are 3 best solutions below

2
zerkms On BEST ANSWER

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.

10
Pratik Joshi On

Use concatenation well.

You should use:

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>'); 
}

Take your Fiddle :)

0
Kiran Varsani On

Try following code instead.

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>');

}