3

I've been thinking about this for a couple of days already.

For some reason my semantic-ui jQuery doesn't work.

So here's what I did.

On my webpack/environment.js:

const { environment } = require('@rails/webpacker')

const webpack = require('webpack')
environment.plugins.append('Provide', new webpack.ProvidePlugin({
  $: 'jquery',
  jQuery: 'jquery'
}))

module.exports = environment

I added a custom.js file with the following codes under app/javascripts/packs:

$(document).on('turbolinks:load', function(){
    $('.ui.dropdown').dropdown();
})

And then inside of my application.js w/c is under app/javascripts/packs I have the ff:

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")

require("packs/custom")

On chrome console here's what I see:

Uncaught TypeError: $(...).dropdown is not a function
    at HTMLDocument.<anonymous> (custom.js:3)
    at HTMLDocument.dispatch (jquery.js:4588)
    at HTMLDocument.elemData.handle (jquery.js:4408)
    at Object../node_modules/turbolinks/dist/turbolinks.js.e.dispatch (turbolinks.js:75)
    at r.notifyApplicationAfterPageLoad (turbolinks.js:994)
    at r.pageLoaded (turbolinks.js:948)
    at turbolinks.js:872

I thought I did it right but for some reason this solution did not work at all. Any idea what am I doing wrong?

1
  • I would guess that the jQuery plugin that supplies the dropdown method has not been initialized at the time you call $('.ui.dropdown').dropdown(). Commented Oct 15, 2019 at 3:08

3 Answers 3

5

Look my /confog/webpack/environment.js file like below:

const { environment } = require('@rails/webpacker')

const webpack = require('webpack')
environment.plugins.prepend('Provide',
    new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        Popper: ['popper.js', 'default']
    })
)

module.exports = environment

And /app/javascript/packs/application.js

require("jquery")
require("bootstrap")
require("packs/custom") // Here is my custom jQuery file like packs/custom.js

and in the /packs/custom.js look like for jQuery preloader

// Preloader JS
jQuery(window).on('load', function () {
    $('.preloader').fadeOut();
});

and exactly this way, custom jQuery is working nicely.

My project based on bootstrap 4 so I run this command for installing dependencies

yarn add bootstrap jquery popper.js 

I think it will help.

Thanks

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

Comments

4
yarn add jquery

in packs/application.js

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

Comments

0

On my webpack/environment.js:

 module.exports = environment
const webpack = require('webpack')
environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jQuery: 'jquery/src/jquery'
  })
)

And /app/javascript/packs/application.js

require("jquery")              //add this in application.js
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")

run this command for installing dependencies

  "yarn add jquery"

Comments

Your Answer

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