I have a method below that requests my server via ajax, the JSON data is returned, and I look through the data and write it in html.
The code works, but it is dirty and inefficient. Is there a way to put html in a template, instead of writing it to javascript code?
thanks
$("[name=btnSearch]").on("click", function () { $.getJSON(ajaxMethod + searchTerm, function (data) { var html = ""; var sel = ""; var name = ""; var type = ""; //alert(id); var css = "ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"; $.each(data, function (key, val) { sel = val.ID; name = val.DisplayName; type = "user"; html += '<tr data-type = "' + type + '" data-result-title = "' + name + '" data-result-id="' + sel + '">'; html += '<td id="' + sel + '">' + name + '</td>'; html += '<td><input type="button" class="select" value="select" style="' + css + '"></td>'; html += '</tr>'; }); //html += '<tr><td style="padding-top: 4px;"> </td><td style="padding-top: 4px;"><input id="btnAddMembers" type="button" value="Add Members" /></td></tr>'; $(html).appendTo('[name=' + div + ']'); }); });
source share