0

I am using jinja to spit out integers into my HTML. I want to convert the integer to USD using JavaScript. My product.cost variable gives the integer 100 and my inttousd() function runs, but does not edit the text inside my HTML. Is there a way to get javascript to update inline or do I have to assign a class to each element and update it that way?

Here is myjinja template:

<script>inttousd("{{ product.cost }}")</script>

Here is a javascript function I made to convert the integer to USD. The console.log shows the function running, but inspect shows everything exactly how jinja saw it except product.cost is given as the integer 100.

function inttousd(str) {
    //Add commas for thousands on first slice
    const [dollars, pennies] = [str.slice(0, -2), str.slice(-2)];
    console.log("$"+dollars+"."+pennies)
    return "$"+dollars+"."+pennies;
}
rockets4all
  • 684
  • 3
  • 8
  • 32
  • 1
    Why not just write it in your Jinja template? It's easily implemented in Python. And yes, `document.write` does what you want in JS, but you probably [don't want to use it](https://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice) if you don't want to get yelled at. – code May 29 '22 at 01:03
  • I'll take your word for it and have my python convert it before giving it to the dom. I'll then have javascript convert back to integer client side for client side math. – rockets4all May 29 '22 at 18:52

0 Answers0