I have a problem using jQuery with Jinja2 + Flask-bootstrap and Flask framework. When I create:
<script>
$('#commentButton').click(function() {
alert('clicked');
});
</script>
I get the following error:
Uncaught ReferenceError: $ is not defined
I can see in Chrome dev tools, that jQuery library is received.
Flask-Bootstrap includes jQuery at the end of your body
tag. If you try to reference it earlier on the page you will receive this error.
The easiest way to make sure your code is placed after jQuery is to override the template tag.
{% block scripts %}
{{ super() }}
<script>
$('#commentButton').click(function() {
alert('clicked');
});
</script>
{% endblock %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With