I’m using a fresh installation of Laravel Framework 9.43.0.
Bootstrap and jQuery are working fine, except voor tooltip(). It’s giving me this error:
Uncaught TypeError: $(…).tooltip is not a function
I tried a lot of different combinations of importing JS libraries. This is my current bootstrap.js:
import _ from 'lodash';
window._ = _;
import $ from 'jquery';
window.$ = window.jQuery = $;
import * as popper from '@popperjs/core';
window.Popper = popper;
import 'bootstrap';
$('body').tooltip({
selector: '[data-bs-toggle="tooltip"]',
});
Dependencies
- jQuery: 3.6.2
- Popper.js: 2.11.6
- Bootstrap: 5.2.3
How can I make tooltip() work?
2
Answers
Use this instead:
Reference:
https://getbootstrap.com/docs/5.0/components/tooltips/#example-enable-tooltips-everywhere
In addition, instead of:
Use this:
Do yourself a favour and get rid of jQuery entirely now you’re in the starting phase of a project. Bootstrap 5 no longer needs jQuery as a dependency, and to be fair as a developer you do not need it anymore either. Almost anything that jQuery offers can be done with a similar amount of code in plain javascript (for instance
$('.myclass')
==document.querySelectorAll('.myclass')
). This makes your code more portable and saves a few cycles on pageload.Using the Bootstrap 5 docs, there is an example given on how to load the tooltips:
https://getbootstrap.com/docs/5.0/components/tooltips/#example-enable-tooltips-everywhere
In other words: attach the
data-bs-toggle="tooltip"
attribute to an element of your choice.