I have a problem similar to this one: Flask+AJAX+Jquery+JINJA to dynamically update HTML Table but can't figure out how to apply the solution to mine. What I need is to automatically update the images displayed in the template based on the response obtained using AJAX for communicating with the Flask backend without reloading it. For this purpose I started using AJAX (but if there are other useful libraries tell me please).
Instead of the {{field}} var in the linked question, I had something like this to be updated (the {{image}} var below) in the HTML:
<ul>
{% for image in list%}
<li>
<img src="{{image}}" height="150" width="150"></img>
</li>
{% endfor %}
</ul>
I did an AJAX call to the Flask entrypoint to get an updated list of images to substitute to the original list:
$.ajax({
url: "...", // Flask entrypoint
type: "POST",
dataType: 'json',
data: {...}, // data I need
success: function(resp){
// Here the resp var contains the new list of images' path.
// but I dont't know how to "replace list var with resp"
}
});
What I would like to do is take the result ("resp") and replace it in place of the images that were there before, as if you replace the "list" variable in HTML with the new "resp" variable. (The "resp" variable, may contains less images than the original ones.)
Update Thanks to Pavlo reply, I did something like this (working):
$.ajax({
url: "/...",
type: "POST",
dataType: 'json',
data: {...},
success: function(resp){
$("ul").children().remove();
for (var i = 0; i < resp.length; i++) {
var elem = "<li><img src=" + resp[i] + "></li>";
$("ul").append(elem);
}
}
});
NEW PROBLEM: The css tags after the Ajax returns, fails to work.
`.... `$('ul#myListId').html(resp)`
– charlietfl Mar 23 '21 at 11:31