0
test.addEventListener('click',function(e){

    console.log("click");
    var targetElement = event.target || event.srcElement;
    clickedVal = targetElement.innerText;
    targetElement.innerText="";
    input = document.createElement("input");
    input.value = clickedVal;
    targetElement.append(input);

    img = document.createElement("img");

    // It's not working here 
    // "{% static 'images/confirm.svg' %}" is incorrect. Then how to correct it?
    img.setAttribute("src", "{% static 'images/confirm.svg' %}");
    input.parentElement.append(img);
})

"{% static 'images/confirm.svg' %}" is incorrect. Then how to correct it? Thanks

UPdate:

img.setAttribute("src", "{% static 'images/confirm.svg' %}");

will result in:

<img src ="{% static 'images/confirm.svg' %}" >

I guess the template engine forgot to translate.

Alston
  • 2,085
  • 5
  • 30
  • 49
  • Try `images/confirm.svg` instead of `"{% static 'images/confirm.svg' %}"` – dineshpandikona Oct 24 '22 at 03:38
  • is this file `.html` or `.js` ? – Zkh Oct 24 '22 at 10:59
  • @Alston, Please share the error log if possible and are u not testing on dev server. If you dont want change the code, why bother asking us on SO? please, try things on dev server then push to production. If its a `.js` file you have to use path image in this way `images/confirm.svg` – dineshpandikona Oct 24 '22 at 12:42
  • @DineshPandikona Using "images/" will work. But the docs didn't say that, it said use {{static}} syntax to access the static files. So ambiguous. It's in the js file – Alston Oct 26 '22 at 03:52
  • @Alston Your script file is just a text file being loaded by the browser. So, browser doesn't know the syntax of Django `{% static %}`. You can follow this SO post https://stackoverflow.com/questions/17893101/reference-static-images-from-javascript-in-django, let me know if your problem persists, I'll write up an answer. – dineshpandikona Oct 26 '22 at 04:09

0 Answers0