-2

I have a Python variable whose value is a string of text and would like to edit that value via Javascript.

I have no idea how to go about doing this.

Attempts:

function changeValue(val) {
  val = 'new text';
}


<textarea placeholder="some text">{{ changeValue({{ result }}) }}</textarea>

<textarea placeholder="some text">
  {{ result }}
</textarea>

What I want: I have some text (result) being added and would like to check if the text is empty. If so, I want to show the placeholder text. The issue: Although I can check if the value is empty, when I try to print that result out it reads none

Thanks to all!

Empty
  • 21
  • 4

1 Answers1

0

You do not need to call the JavaScript function from the HTML file. There are several approaches you can take:

1. Store the variable in HTML metadata:

<meta id="result_var" data-result={{result}}>

And then get the data in JavaScript:

result = document.getElementById("result_var").value;

2. Keep the variable in the tag where it's supposed to be and get it from there in JavaScript:

<textarea placeholder="some text" id="result-var"> {{result}} </textarea>

And then get it in JavaScript:

let result = document.getElementById("result-var");

3. Query it from your API: You can create a route in your Flask app that returns JSON data with the variable you need and then get that data to your JavaScript file by sending a request to your API.

4. Jinja format: I've seen solutions that involve just using the variable as if it was a jinja variable in JavaScript like this: let result = JSON.parse('{{ result | tojson }}');. But I haven't been able to get this working properly, not sure why.

I hope this helps!

Moin
  • 1
  • 2