soby
soby

Reputation: 60

Adding jinja2 template code to javascript innerHTML

I am trying to get javascript to insert a flask-wtf form into a div as such.

var element = 'content'
var addElement = `{{ form.${element}(class="") }}`

var newDiv = document.createElement("div");
newDiv.innerHTML = addElement;

Javascript displays my addElement as a string on my website instead of interpreting it to a form. I suspect this is because of the jinja2 syntax in there. How do i get it to display as a form?

Upvotes: 0

Views: 1163

Answers (2)

Hassanein ali
Hassanein ali

Reputation: 3

unfortunately you simply can't. but you can write the form (or any jinja code) in an element in your HTML code and assign its display to none and then show it to the user when you like to from javascript

Upvotes: 0

napuzba
napuzba

Reputation: 6288

You can escape the javascript code with {% raw %} and {% endraw %}

{% raw %}
var element = 'content'
var addElement = `{{ form.${element}(class="") }}`

var newDiv = document.createElement("div");
newDiv.innerHTML = addElement
{% endraw %}

Upvotes: 0

Related Questions