0

I'm trying to refactor js script without using jquery. And the most difficult part, to me, is to understand why addEventListener is not equivalent to $().on(). I need to add some listeners to a modal which is loaded after the DOM is built. So I did:

document.addEventListener("DOMContentLoaded", function(){
    
    document.getElementById('form-modal').addEventListener('show.bs.modal', function () {
       // Here the js logic relevant to the modal     
       console.log('loaded by pure js')
    })

    $('#form-modal').on('show.bs.modal', function () {
        console.log('with help of jquery');
    })

});

Why the pure javascript doesn't work while the jquery does? What am I missin?

Emilio Conte
  • 1,105
  • 10
  • 29
  • 1
    Check https://blog.carsonevans.ca/2020/08/08/bootstrap-modals-without-jquery/ – James Jan 03 '23 at 13:18
  • 1
    [jQuery events work differently](https://stackoverflow.com/a/2518441/10304804). Bootstrap 3 and 4 are only using the jQuery events, but [Bootstrap 5](https://getbootstrap.com/docs/5.0/getting-started/javascript/#still-want-to-use-jquery-its-possible) is designed to work without jQuery and [uses native handlers](https://getbootstrap.com/docs/5.0/components/modal/). Maybe upgrade? – Christopher Jan 03 '23 at 13:21
  • @Christopher +1 Big job in perspective! Thanks – Emilio Conte Jan 03 '23 at 14:13

0 Answers0