最白目
最白目

Reputation: 3654

Uncaught SyntaxError: Unexpected identifier with .append() string

I keep getting this error in the $('#savetickets-list') line. I want to dynamically add fields to a table, the table has the id in HTML.

<div class="savetickets-list">
</div>

In javascript I fill the table in a for loop

for (var i = 0; i < len; i++) {
    // the data comes from a web database
    var ticketname = results.rows.item(i).iTicketName;

    $('#savetickets-list').append('
        <div class="saveticket gradient-top">
            <h3>' + ticketname + '</h3>
        </div>
    ');
}   

I dont know how to solve this. jQuery is loaded, I also checked the name of the selector.

Please help.

Upvotes: 0

Views: 3198

Answers (3)

Piotr Olaszewski
Piotr Olaszewski

Reputation: 6224

$('#savetickets-list').append('\
        <div class="saveticket gradient-top">\
            <h3>' + ticketname + '</h3>\
        </div>\
    ');

when you want to write multiline strings in JS, you must escape new lines.

Upvotes: 5

Cranio
Cranio

Reputation: 9848

Just checked. The problem is in the newlines, you have to concatenate strings or put all the statement in a single line.

Upvotes: 0

Sammaye
Sammaye

Reputation: 43884

It is because you are using new lines.

JS does not automatically read new lines for you. It treats them as new statements.

The way I prefer to do this is like:

$('#savetickets-list').append('<div class="saveticket gradient-top">'+
    '<h3>' + ticketname + '</h3>'+
'</div>');

Upvotes: 3

Related Questions