0

I installed a fresh copy of Laravel 8 but I am unable to use $ in jQuery. I know that this is a frequent question here in SO link this but I am unable to jQuery in document ready.

Here are the steps that I have tried based on what I read in various SO and blogs:

npm install jquery --save-dev (someone said that jquery was removed from laravel 6 in this)

npm install jquery-ui --save-dev

added in main.blade.php (under /body)

<script src="{{ asset('js/app.js') }}"></script>

in bootstrap.js

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

in app.scss

@import "~jquery-ui/themes/base/all.css";

webpack.mix.js

mix.js("resources/js/app.js", "public/js")
    .sass("resources/sass/app.scss", "public/css")
    .sass("resources/scss/normalize.scss", "public/css")
    .sass("resources/scss/main.scss", "public/css")
    .sourceMaps();

run npm run dev

I know this is a very basic question but I don't know how to proceed.

EDIT I got it working. I added <script src="{{ asset('js/app.js') }}"></script> under </body> it should have been inside <head></head>. The problem was when the app.js should have been declared.

2
  • Try this Commented Jun 27, 2022 at 6:15
  • Thank you for reply. I did consider using cdn instead of using the npm install route but I wish to use npm install. If I am still unable to fix this by the end of the day, I will try the cdn route later. Commented Jun 27, 2022 at 6:18

1 Answer 1

0

Kenneth, Please put this line at starting of 'resources/js/app.js' instead of bootstrap.js

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

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

2 Comments

Yes. I also tried adding this in app.js and/or bootstrap.js. Same result
also, in app.js, require("./bootstrap"); was already declared and inside bootsrap.js, window.$ = window.jQuery = require("jquery"); was also declared

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.