title: Call a JavaScript function from jinja2 template for loop button
body: <p>I am learning Jinja and JS. I have few submit buttons created via for loop in Jinja template:</p>
  <pre><code>{% for item in items %}
  &lt;input type=&quot;submit&quot; value=&quot;Place your Order&quot; class=&quot;submit-class&quot; /&gt;
  {% endfor %}
  </code></pre>
  <p>I want to call JavaScript when any of the button is clicked. I tried below but it gives me error</p>
  <p>**
  Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
  at window.onload
  **</p>
  <p>Here is my JS code:</p>
  <pre><code>window.onload = function() {
      document.querySelector(&quot;.submit-class&quot;).addEventListener(&quot;click&quot;, function() {console.log(&quot;liked this product&quot;);
          });
  };
  </code></pre>
  <p>Any help would be great.</p>