kidrek
kidrek

Reputation: 11

Jinja2 -- use a javascript variable in my jinja template

I want to make a dynamic filter on an element of choice (HTML SELECT element) in my web page.

My first list was already set, and I want to change the content of my second list (HTML select element) over my first choice.

But to do this, i need to use the result of document.getElementByid ( javascript ) in jinja2 as a dynamic variable like this:

document.getElementById('select02').innerHTML = 
"{% for i in context.get(document.getElementById('select02').value) %}
<option {{'selected' if package == 1 else ''}} value='{{i}}'>{{i}}</option>
{% endfor %}";

To generate page, i'm using a python script with jinja2 template.

Has someone already used this feature?

Upvotes: 1

Views: 3757

Answers (1)

thkang
thkang

Reputation: 11543

Assuming context is a dict, you have to put everything of that into your page maybe via JSON. Then you need to write javascript functions to 1) fetch what user have selected for the first list(list means html select element here) 2) use value from 1) to update the second list. using javascript events it'll be best to have 2) fired upon every user interaction (change, click) for the first list.

remember, you can't mix server-side code(jinja2 template) with client-side code(javascript)!

Upvotes: 3

Related Questions