1

With the last version of Laravel, I cannot use a simple jQuery plugin.

‍I try to use ‍‍‍bootstrap-datepicker plugin. So I used:

yarn add bootstrap-datepicker

Then, in my app.js:

import './bootstrap';
import 'bootstrap-datepicker';

In bootstrap.js, I have:

try {
    window.$ = window.jQuery = require('jquery');

    require('bootstrap');
} catch (e) {}

This should make jQuery global I guess.

But when I try:

$(function() {
        $('.input-daterange input').each(function() {
            $(this).datepicker('clearDates');
        });
});

I got the error:

app.js:41747 Uncaught TypeError: $(...).datepicker is not a function

I have this error with a lot of plugins (daterangepicker, datepicker...)

What I have to do to use Webpack / Mix to just use one simple jQuery plugin?

4
  • refer this justin.ly/adding-jquery-bootstrap-plugins-laravel-5-mix Commented Sep 12, 2018 at 18:18
  • Nice explanations but I got same problems using his code... Commented Sep 12, 2018 at 18:25
  • did you run mix ? npm run dev Commented Sep 12, 2018 at 18:27
  • Yes of course, when I use mix.autoload, nothing work anymore :( Commented Sep 12, 2018 at 18:30

2 Answers 2

1

I made it this way:

In console:

npm i jquery-ui-dist

/resources/assets/js/bootstrap.js:

try {
    window.$ = window.jQuery = require('jquery');
    require('bootstrap');
    require('jquery-ui-dist/jquery-ui')
} catch (e) {}

/resources/assets/sass/app.scss:

@import'~jquery-ui-dist/jquery-ui';

In your blade or at the end of boostrap.js

$('.input-daterange input').datepicker(params)

In this case I'm usign jQuery UI DatePicker

Sign up to request clarification or add additional context in comments.

Comments

0

If jQuery and jQuery Plugin not in the same file, you need to use $.fn.extend() merge the contents of plugin object onto the jQuery prototype to provide new jQuery instance methods.

$.fn.extend({
    datepicker: function() {
        require('bootstrap-datepicker');
    }
});

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.