I'm currently migrating an ES6 project from Bootstrap 4 to Bootstrap 5 and get this error:
Uncaught TypeError: bootstrapElement.Tooltip is not a function
As stated in the Migration Notes, Bootstrap 5 has dropped the default integration of jQuery. However, Bootstraps jQuery plugins will still be loaded, if jQuery is detected in the window
object.
In my case, I import jQuery, but Bootstrap does not seem to detect it:
import $ from "jquery";
import {Tooltip} from "bootstrap";
import {getjQuery} from "bootstrap/js/src/util";
...
console.log(getjQuery()); // Returns null
bootstrapElement.Tooltip({ title: "Element Tooltip" }); // Throws the above error
But even if I try to load jQuery into the window object manually, the error occurs:
if(!getjQuery()) {
window.jQuery = window.$ = $;
}
console.log(getjQuery()); // Returns jQuery now
bootstrapElement.Tooltip({ title: "Element Tooltip" }); // It still throws the error
I guess this is because I load jQuery into window
after the import of Tooltip
and the plugin would be loaded during the import. Which it won't, because jQuery is not set to window
yet.
I'm aware that I could use the plugin directly by using
let elementTooltip = new Tooltip(bootstrapElement, { title: "Element Tooltip" });
elementTooltip.show();
instead of
bootstrapElement.Tooltip({ title: "Element Tooltip" });
bootstrapElement.Tooltip("Show");
but then I encounter problems later when I try to remove the tooltip again, because I can't access elementTooltip
anymore and creating a new instance won't do the trick. Resolving this would mean a lot of work in the architecture of the project (mostly because this is not the only plugin I use) and I'd like to avoid that.
Is there a possibility to import jQuery in a way that Bootstrap 5 is able to detect it and load its jQuery plugin?