4

I have 4 files for my page - html.html, css.css, javascript.js, and jquery.js. In my jquery.js file I am having issues as when I use the starting code it comes up with an error in the console. Here is the code:

$(function(){console.log("works")});

For some reason this comes up in the console log:

Uncaught ReferenceError: $ is not defined

Anyone know why and how to fix it? I think it may be related to the shorthand of the "document" thing and maybe because it is a separate file it doesn't work.

Finley Sherwood
  • 323
  • 3
  • 5
  • 14

2 Answers2

3

It looks like you came across the solution so I'm going to still post this here for others to see:

It's highly likely you did not link the jQuery file first.

This would give the error you describe:

    <script type='text/javascript' src='script.js'></script>
    <script src='https://code.jquery.com/jquery-3.1.0.min.js'></script>

This would be correct:

    <script src='https://code.jquery.com/jquery-3.1.0.min.js'></script>
    <script type='text/javascript' src='script.js'></script>

The reason why: your script.js uses jQuery. If you load the script.js first, jQuery specific delimiters, functions, etc will not be recognized in the script because they have not been defined yet.

Montag
  • 545
  • 1
  • 4
  • 12
1
import $ from '../../assets/splitar/jquery-1.11.1.min.js';

Here you can replace your jquery store dir path and re run the project then your problem will sloved.

Tismember
  • 11
  • 2