Uncaught TypeError: $(...).tooltip Is Not A Function In Laravel Application (Bootstrap 4 Preset)
I am building an application with the PHP framework Laravel. I am using the Bootstrap 4 preset and installed a fresh project. Everything is working fine, css, and even the dropdown menu you get from the navbar. But for some reason, I get a Typerror in my console when I try to initatlize the tooltip by doing this. Also tried to initalize the tooltip by a tooltip class but that doesn't seem to work either. I got the same problem with the popover component.
Uncaught TypeError: $(...).tooltip is not a function
Uncaught TypeError: $(...).popover is not a function
I've changed nothing in the JS, except deleting the comments you get from a Laravel installation.
bootstrap.js
window._ = require('lodash');
//Popper for Jquery/Bootstrap
window.Popper = require('popper.js/dist/umd/popper');
//Jquery for Bootstrap
try {
window.$ = window.jQuery = require('jquery/dist/jquery.slim');
require('bootstrap');
} catch (e) {}
//Axios for AJAX Calls
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
let token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
app.js
//Bootstrap application with dependencies
require('./bootstrap');
//Import Vue
window.Vue = require('vue');
//Vue components
Vue.component('example-component', require('./components/ExampleComponent.vue'));
//Vue init
const app = new Vue({
el: '#app'
});
//Tooltip init
$(function () {
$('[data-toggle="tooltip"]').tooltip()
})
HTML/Blade
<i class="ion-help-circled" data-toggle="tooltip" data-placement="top" title="Dit is het adres waarop de website staat tijdens de testfase"></i>
Also tried to include the tooltip init below the regular scripts in my blade file, but again, doesn't seem to work.
<!-- Scripts -->
<script src="{{ asset('js/app.js') }}"></script>
<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip()
});
</script>
It seems that it is a problem with popper.js I guess, because it applies to both the tooltip and the popover component.
Answer
Try instead
window.$ = window.jQuery = require('jquery'); // <-- main, not 'slim'
window.Popper = require('popper.js');
require('bootstrap');
The "slim" jQuery dist excludes some features, necessary to register correctly tooltip
as a jQuery plugin.
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM