0

I have laravel application and trying to use dialog widget, but I keep getting this error in the console:

TypeError: $(...).dialog is not a function[Learn More]

I tried to test normal jquery functions and it works fine, it is just jquery UI.

    <head>

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('app.name', 'Laravel') }}</title>

    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">

    <!-- Scripts -->
    <script>
        window.Laravel = {!! json_encode([
            'csrfToken' => csrf_token(),
        ]) !!};
    </script>
   <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="/resources/demos/style.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    <script type="text/javascript">
    $( function() {
    $( "#dialog" ).dialog({
      autoOpen: false,
      show: {
        effect: "blind",
        duration: 1000
      },
      hide: {
        effect: "explode",
        duration: 1000
      }
    });

    $( ".opener" ).on( "click", function() {
      $( "#dialog" ).dialog( "open" );
    });
    } );
   </script>
     </head>


    <body>
     <div id="dialog" title="Basic dialog">
    <p>This is an animated dialog which is useful for displaying information.   The dialog window can be moved, resized and closed with the 'x' icon.</p>
     </div>

Update:

there is a conflict between these lines:

  <script src="{{ asset('js/app.js') }}"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

but still can't remove any of them.

Update 2

webpack.mix.js

const { mix } = require('laravel-mix');

mix.js('resources/assets/js/app.js', 'public/js')
   .sass('resources/assets/sass/app.scss', 'public/css');

app.js

require('./bootstrap');

window.Vue = require('vue');

Vue.component('example', require('./components/Example.vue'));

const app = new Vue({
    el: '#app'
});

bootstrap.js

window._ = require('lodash');

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

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

window.axios = require('axios');

window.axios.defaults.headers.common['X-CSRF-TOKEN'] = window.Laravel.csrfToken;
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
8
  • On exactly which line do you get this error? Commented May 14, 2017 at 20:06
  • $( "#dialog" ).dialog({ Commented May 14, 2017 at 20:06
  • I am unable to reproduce the problem with your current supplied code Commented May 14, 2017 at 20:20
  • let me ask you something, does app.js application loads a copy of the jquery ui or something, maybe it is a conflict between two copies Commented May 14, 2017 at 20:22
  • You you tried looking at the full page output? Use your browsers inspect function to see all loaded js files Commented May 14, 2017 at 20:23

3 Answers 3

2

See this link: https://github.com/JeffreyWay/laravel-mix/blob/master/docs/jquery-ui.md

NOTE: I am fairly new to Laravel Mix (webpack) and still learning.

In the linked example his resources/assets/js/app.js contains the lines:

import $ from 'jquery';
window.$ = window.jQuery = $;

but in my case I'm editing resources/assets/js/bootstrap.js, which contains the line:

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

so I added the widget import line:

import 'jquery-ui/ui/widgets/dialog.js';

After the edits, to run the Mix tasks, I ran this command at my project root: npm run dev

And in my Chrome browser, on the page calling $(...).dialog(), I opened the devtools console and then right clicked on the browser page reload icon and clicked Empty cache and hard reload

Since I am using Laravel Mix (webpack) I do not need to include the jquery specific script tags and stylesheet links.

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

Comments

0

Are you using the .sass file that you're mixing in your webpack.mix.js file?

If not, try changing your webpack.mix.js to this:

const { mix } = require('laravel-mix');

mix.js('resources/assets/js/app.js', 'public/js');

New to Laravel and web development in general; that worked for me.

Comments

0

Try inserting app.js before jquery-ui.js. This makes jquery-ui.js overwrite any common code. I found it does not cause significant side-effects. Some items like tooltips will look different, but you can just modify the linked js files to handle that if necessary.

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.