I want to call the javaScript function defined inside the same template in Django. How can I do it?
{% extends 'base.html' %}
{% block content %}
{% if error %}
showAlert()
{% endif %}
<script> function showAlert() {alert ("Please select at least one option");}</script>
{% endblock %}
I want to call showAlert() if there is error present. I have handled the error in the view. I do not a method about how to call the function here? It is showing the function name.
asked Sep 7, 2019 at 9:14
Deshwal
4,28212 gold badges57 silver badges119 bronze badges
1 Answer 1
You can call the javascript function inside the <script> tag
{% extends 'base.html' %}
{% block content %}
<script>
function showAlert() {
alert ("Please select at least one option");
}
</script>
{% if error %}
<script>
showAlert()
</script>
{% endif %}
{% endblock %}
Or you can put {% if ... %} statement inside <script> tag
Sign up to request clarification or add additional context in comments.
1 Comment
Deshwal
Thanks for helping out buddy but the problem is that it does not let the page to load until clicked okay.
lang-js
alertis available in the browser in JavaScript.<script>blocks.<script> showAlert() </script>