0

I have a script that overrides autocomplete

<script type="text/javascript">
$(function() {
$("#autocomplete").autocomplete({
    source:function(request, response) {
        $.getJSON("{{url_for('user.autocomplete')}}",{
            search: request.term, 
        }, function(data) {
            response(data.results); 
        });
    },
    minLength: 2,
    }
});
})

</script>

Later in my code i have the following form

<form method=post action="{{ url_for('user.merge_user')}}">
    <input name="autocomplete" type="text" id="autocomplete" class="form-control input-lg", length="20"/>
    {{ form.user_name(id="autocomplete", class="form-control input-lg") }}
    <input class="btn btn-default" type=submit value=Register>
</form>

The HTML input uses the script as desired but the Jinja version uses the default autocomplete method. How can I get it to use the script?

0

1 Answer 1

2

This is probably the solution you are looking for. It enables Jinja2 to load the JavaScript file before HTML is rendered.

{% block javascript %}
    <script type="text/javascript">
        {% include "script.js" %}
    </script>
{% endblock %}

Also, a similar question can be found here.

Note that if you are using Flask with this solution (which you are), you may experience some issues related to Jinja not finding the JavaScript file on the server. IDK how to fix this; was too lazy anyways.

:3

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.