Search code examples
javascriptquotesdouble-quotes

how to write single and double quotes while appending data?


 $(".overlayp ul").append('<li id="group_'+msg1[i][0].id+'" class="list_items"><div class="row"><div class="col-xs-2 col-md-1"><div class="itemAvatar"><img src="'+url+'/service/getUserImage/'+msg1[i][0].id+'/32" alt="avatar"></div></div><div class="col-xs-10 col-md-11"><div class="item"><div class="item-title">'+msg1[i][0].name+'<span class="pull-right members"><a onclick="if(confirm("Are you sure?"))
removegroupuser('+groupid+','+msg1[i][0].id+');">Remove User</a></span></div></div></div></div></li>');

i am getting the unexpected token error because while appending the li i have started with single quote while writing the confirm dialog box facing a problem how could i mention the quote inside "Are you sure?" basically a quotes problem in this onclick="if(confirm("Are you sure?")) removegroupuser('+groupid+','+msg1[i][0].id+');"


Solution

  • Change the quotes in confirm to single apostrophes and escape them:

    onclick="if(confirm(\'Are you sure?\')) removegroupuser('+groupid+','+msg1[i][0].id+');"
    

    Or use HTML entities:

    onclick="if(confirm(&quot;Are you sure?&quot;)) removegroupuser('+groupid+','+msg1[i][0].id+');"