Using jQuery with a flask
I have a problem using jQuery with Jinja2 + Flask-bootstrap and flash framework. When I create:
<script>
$('#commentButton').click(function() {
alert('clicked');
});
</script>
I am getting the following error:
Unprepared ReferenceError: $ undefined
I can see in Chrome dev tools that the jQuery library has been fetched.
source to share
Flask-Bootstrap includes jQuery at the end of the tag body
. If you try to link to it earlier on the page, you will get this error.
The easiest way to make sure your code is posted after jQuery overrides the template tag.
{% block scripts %}
{{ super() }}
<script>
$('#commentButton').click(function() {
alert('clicked');
});
</script>
{% endblock %}
source to share
The error indicates that it is $
not defined. Maybe jQuery is not included at all, but since you say that it is included, most likely it must have been renamed to a different name, check it out.
Meanwhile try using jQuery
instead $
, if it works you can do $ = jQuery
which will pass the link from jQuery
to $
, so you can use$
source to share