Search code examples
javascriptpythontornado

Executing javascript in python when condition is met


I have a webchat application which is running on python, I'm trying to run a different javascript function for each room, something similar to what I've done with the "active" class in css.

<li {% if room == 1 %}class="active"{% end %}><a href="/room/1">Room 1</a></li>

Here's my attempt at creating a solution:

<li {% if room == 1 %}click="myFunction()"{% end %}><a href="/room/1">Room 1</a></li>

Basically, when someone visits room 1, the myFunction() javascript will be executed. Any ideas as to how one could achieve this?


Solution

  • Based on that this is working exactly how you want:

    <li {% if room == 1 %}class="active"{% end %}><a href="/room/1">Room 1</a></li>
    

    Then to make the JS example work you want:

    <li {% if room == 1 %}onclick="myFunction()"{% end %}><a href="/room/1">Room 1</a></li>
    

    Notice that it's onclick not just click. If you were using addEventListener it would just be click, but when putting an event listener on an element directly like that, it's always (to the best of my knowledge) preceded by on, hence: onclick.