var $template;
$.get('/templates/searchTemplate.tmpl', function(HTMLres) {
$template = $(HTMLres);
});
$.each(data.results, function(key, value) {
$template.find('button').off().on('click', function() { console.log('you clicked me') });
$template.find('.media-body > strong').html(value.firstname + ' ' + value.lastname);
$template.find('.email').html(value.mail);
$template.find('.phone').html(value.phone);
$template.find('.age').html(value.age);
$('#searchRsults').append($template);
});
When I try to use this code instead of having the template file to be Appended to the UL element, this will clear the previous append and append the next result to the UL element.
The template file is just a -> <LI> </LI>
with some spans.
Note that if I put the |$.get (template)
in the $.each
part of the code everything works like it supposed to be all the <LI> </LI>
element are appended one after another.
But doing so it makes a lot of request for the template file. On every iteration actually.
Even if I use the .DONE
after the Ajax request still the result is the same the <LI> </LI>
gets overwritten not appended