9

I have a header with dropdown-menu. If I click, I will see the menu. All work perfectly. Here is the HTML: enter image description here

Another page I had import modal from bootstrap and nothing happens when I click on the dropdown. In the console it doesn't show any errors, but I don't know why, I have 2 events: enter image description here

If I remove my import from Js. file, it will work perfectly:

import { Modal } from 'bootstrap';

app.js

const bootstrap = require('bootstrap/dist/js/bootstrap.bundle');

contact.js

import { Modal } from 'bootstrap';


window.addEventListener("DOMContentLoaded", () => {
if (document.getElementById('emailHasBeenSent') !== null && document.getElementById('emailHasBeenSent').value) {
    const modal = new Modal(document.getElementById('contactUsModal')),
        closeModalBtn = document.querySelector('.contact--modal-close-btn');

    modal.show();

    closeModalBtn.addEventListener('click', e => {
        modal.hide();
    })
}});

Modal of Bootstrap seems to "cancel" onClick event dropdown. The class name stay on "btn dropdown-toggle" and not "btn dropdown-toggle show". Could you help me to find where is the problem.

2 Answers 2

13

I had a similar problem. Importing straight from 'bootstrap' causes the dropdown listeners to disappear.

Fixed it by using deeper import: import Modal from 'bootstrap/js/dist/modal';

Goes to category: I dont know why but it works.

6
  • I've been struggling so long to even find that this was the issue... very helpful workaround!
    – Randy
    Jul 5, 2021 at 17:44
  • You just saved my day, dude, I've been struggling with this issue for hours!
    – eyho
    Jul 9, 2021 at 12:33
  • 1
    programmer who has been staring at this too long note: It HAS to be import Modal from 'bootstrap/js/dist/modal';. import { Modal } from 'bootstrap/js/dist/modal'; fails. Feb 21, 2022 at 22:04
  • 2
    I'm loathe to jump in with a "me, too" comment, but I've been fighting this for hours, I thought it was related to jQuery and I was hacking this way and that. This should be in the bootstrap documentation, it's incredible that importing a modal the documented way would break dropdowns. Thanks!! Mar 10, 2022 at 16:11
  • THANKS! I've spent hours trying to find why TWO of my pages won't work. After boiling it down to the import, it did not look good. But finding this post saved the day :-) Thanks! Oct 30, 2023 at 17:02
0

i had a same problem. I had loaded bootstrap twice inside another file that comes from xmlHttpRequest. may by you had a same mistake

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 5, 2022 at 7:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.