NWOWN
NWOWN

Reputation: 409

How can I print element of array from Flask on javascript on HTML

I want to print element of array from Flask on javascript in HTML.

Here is my javascript code in HTML.

    <script type="text/javascript">  
      var a=1;
      var test_array = new Array(1000);
      var i=0;
    {% for info in results %}
        test_array[i]={{ info[2] }};
        i+=1;
    {% endfor %}
    </script>

    <script>
       document.write(a);
       document.write(test_array[2]);
    </script>

'results' is a variable that receives a value from Flask, and the value is stored in 'test_array'.

I want print element of 'test_array'.

But it isn't print and is it really stored?

What can I do?

I would appreciate your advice.

Upvotes: 1

Views: 1162

Answers (1)

Grey Li
Grey Li

Reputation: 12762

You can't combine Jinja's for loop with Javascript's variable i.
Instead, you can try to use Jinja's loop.index0 variable (0 indexed), see documentation here:

{% for info in results %}
test_array[{{ loop.index0 }}] = "{{ info[2] }}";
{% endfor %}

( Not test yet, give it a try. )

Upvotes: 1

Related Questions