Search code examples
javascriptjqueryquotesquoting

jQuery append: can't get the quotes right


I'm trying to add some HTML to a div, but I can't get the quotes right... This is what I thought would work, but doesn't...:

success: function (data) {
id = data.photo_id;
$("#successtext").append('<a href="https://www.theurl.com/' + id + '" data-role="button">The link</a><a href="#" data-role="button">Log Out</a>');
$("#success").show();
$("#uploading").hide();
$("#turn").hide();
turnable = 0;
}

As you can see, this is part of an Ajax calls. Note that the append does work, just not with the right button markup.

Note: the first link links to, for example, https://www.theurl.com/223424324

What am I doing wrong, and why? Thanks!


Solution

  • you have extra quote at www.theurl.com/"'+id+'", change to:

    $("#successtext").append('<a href="https://www.theurl.com/'+id+'" data-role="button">The link</a><a href="#" data-role="button">Log Out</a>');
    

    Update:: call .trigger('create') after appending

    $("#successtext")
        .append('<a href="https://www.theurl.com/'+id+'" data-role="button">The link</a><a href="#" data-role="button">Log Out</a>')
        .trigger('create');
    

    Demo:: jsFiddle