For the love of it, I read through many posts (1, 2 ...) and I could still not solve this simple problem:
I have a button, that opens a modal using htmx
:
<button class="btn btn-primary"
id="createButton"
hx-get="/create"
hx-target="#modals-here">create
</button>
My modal looks like this and opens and functions perfectly:
<div id="modal-backdrop" class="modal-backdrop fade show" style="display:block;"></div>
<div id="modalCreate" class="modal fade show" tabindex="-1" style="display:block;">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-body">
{% csrf_token %}
{% crispy form %}
</div>
</div>
</div>
</div>
and somewhere on my page I have a <div id="modals-here"></div>
.
And in this modal there are input fields I'd like to validate. For this purpose it would be extremely useful to know when the modal is opened. I tried:
$('#modalCreate').on('shown', function (event) {
//('#modalCreate').on('show.bs.modal', function (event) {
console.log("hello");
});
This is my base.html
(for the includes):
{% block javascript %}
<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="..." crossorigin="anonymous"></script>
<!-- Bootstrap JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="..." crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="..." crossorigin="anonymous"></script>
<!-- htmx -->
<script src="{% static 'js/htmx.min.js' %}"></script>
<!-- crispy validator -->
<script src="{% static 'js/crispy_validator.js' %}"></script>
{% endblock javascript %}
I need a reliable way to determine if / which of my modal is active. The hello
never appears in the console.