question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Modal events not working for JQuery .on()

See original GitHub issue

Context : Symfony 5.3.9 project / Boostrap 5.1.3

This works : document.getElementById('myModal').addEventListener('hidden.bs.modal', function() { alert('ok'); });

This doesn’t : $("#myModal").on('hidden.bs.modal', function() { alert('ok'); });

Same goes for any modal event. Did I miss something ?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
alecplcommented, Nov 23, 2021

The detection depends on existence of window.jQuery. I guess, in some cases it might just not exist. As this is mentioned in https://getbootstrap.com/docs/5.0/getting-started/javascript/#still-want-to-use-jquery-its-possible (and #events) I suggest to close this ticket. Maybe every page mentioning addEventListener() should have a link (“How to do this with jQuery?”) to this getting started page section.

ps. I once worked with cash-dom which is a tiny jQuery replacement. The way it was imported created only window.$.

2reactions
nkdas91commented, Oct 29, 2021

@UgoFlamarion Did you include jQuery? If not please include it and try again.

<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

And if it doesn’t work, please create a https://codepen.io/ demo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bootstrap modal event not fired - Stack Overflow
I'm creating my modal's dynamically, so they don't exist on the page load. I'm adding them to the DOM with a JQuery .append('body')...
Read more >
JQuery click event not working in bootstrap modal - YouTube
jQuery : JQuery click event not working in bootstrap modal [ Beautify Your Computer : https://www.hows.tech/p/recommended.html ] jQuery ...
Read more >
JavaScript - Bootstrap
Use event.relatedTarget and HTML data-* attributes (possibly via jQuery) to vary the contents of the modal depending on which button was clicked. See...
Read more >
Modal event.preventDefault() for show.bs.modal ... - GitHub
This is because the modal.js module sets the value for whether the modal is transitioning or not before it triggers the show.bs.modal event....
Read more >
jQuery not working within my modal - Laracasts
I need to use jQuery to call a function on show of my Bootstrap Modal. Copy Code $('#Modal').on('show.bs.modal', function (event) {... This usually...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found