Chung Do Chung Do - 3 months ago 7
jQuery Question

How to specific variable value inside "" javascript

My code

success: function(result) {
var htmlString = '';
htmlString+= '<div class=\"row mgt45 bg-delete\"><div class=\"col-sm-5\"><div class=\"p-relative\"><img id=\"logoImg\" src=\"result.image\" width=\"100%\"><button id=\"result.id\" class=\"change-img topleft\" data-toggle=\"modal\" data-target=\"#myModal\"><i class=\"fa fa-camera\" aria-hidden=\"true\"></i> 画像</button></div> </div><div class=\"col-sm-7 corp-office\"><h2 id=\"title_result.id\" class=\"changeableFacility\" contenteditable=\"true\">result.title</h2><p id=\"content1_result.id\" class=\"changeableFacility\" contenteditable=\"true\">result.content1</p><p id=\"result.content2\" class=\"changeableFacility\" contenteditable=\"true\">result.content2</p></div><span id=\"delete_result.id\" href=\"\" class=\"btn-delete\"><i class=\"fa fa-times-circle\" aria-hidden=\"true\"></i></span> </div>';

$('#addItem').before(htmlString);
},


How can I store
result.id
,
result.image
as a variable instead of string?

Answer

Use ECMAScript 2015's Template String Literals

var id = 5;
var image = "myimage.jpg";
alert(` <div id=\"${id}\">`);
alert(` <img src=\"${image}\">`)