Issue
I have a html page runs with flask framework and I use jinja2 to ouptput partial content:
<script>function test(str){ alert(str) }</script>
{% set s = 'Hello! Jinja~' %}
<a href='#' onclick='test({{s}})'>click me</a>
How could the page alert
Hello! Jinja~
while the link is clicked?
Current the html ouput just like:
<a href="#" onclick="test(Hello! Jinja~)">click me</a>
This causes javascript error...
The variable s is got from db or other handling before page render and not a constant string!
Thanks much...
Solution
Surround the Jinja expression in quotes so that it is treated as a string in JavaScript.
{% set s = 'Hello! Jinja~' %}
<a href='#' onClick="alert('{{ s }}')">click me</a>
Answered By - vnk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.