javascript - Uncaught SyntaxError: Unexpected identifier with .append() string - Stack Overflow

I keep getting this error in the $('#savetickets-list') line. I want to dynamically add field

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 es 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.

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 es 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.

Share edited Jun 28, 2012 at 7:57 Esailija 140k23 gold badges279 silver badges328 bronze badges asked Jun 28, 2012 at 7:51 最白目最白目 3,6549 gold badges64 silver badges116 bronze badges 4
  • 1 What is the value of ticketname? Can you post the contents of results.rows in your question. – Rory McCrossan Commented Jun 28, 2012 at 7:54
  • 1 This looks for an id: $('#savetickets-list'), while you have class="savetickets-list" in your markup. – Jared Farrish Commented Jun 28, 2012 at 7:55
  • I have edited the title so it's more obvious that the code doesn't even run in the first place (Syntax Error, cmon people..) – Esailija Commented Jun 28, 2012 at 7:58
  • @Rory McCrossan: It also didnt work withouth the "ticketname" in the .append('..content..') content. So I think it´s ok if I save the work of posting it, but thanks anyway – 最白目 Commented Jun 28, 2012 at 8:00
Add a ment  | 

3 Answers 3

Reset to default 5
$('#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.

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744857695a4597505.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信