1

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.

1
  • What the values in info[2] ? If it's string you have to add quotes like test_array[i]="{{ info[2] }}"; (and surelly escape possibles quotes in value) Commented Feb 1, 2017 at 9:12

1 Answer 1

1

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. )

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.