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.

+3


source to share


2 answers


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 %}

      

+9


source


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$

0


source







All Articles