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.

Fin
  • 353
  • 3
  • 5
  • 16
  • 2
    Is jQuery included before this file? – ceejayoz Jan 07 '17 at 02:41
  • What does this mean? the entire file for my jquery.js is that. I only just started using separate files so Idk everything about it – Fin Jan 07 '17 at 02:42
  • Try reading this question http://stackoverflow.com/questions/2194992/jquery-is-not-defined – Benjamin Jan 07 '17 at 02:42
  • @FinleySherwood You say you have `javascript.js` and `jquery.js`. One of them presumably contains jQuery, the other presumably contains your code that *uses* jQuery. Which one is included first? – ceejayoz Jan 07 '17 at 02:44
  • Ok I found out why. The script source line for jquery.js was before the one for jquery. Thanks! – Fin Jan 07 '17 at 02:44
  • Put a min js pn the top of the all js [here](https://cdnjs.com/libraries/jquery/) you can find the cdn – Curiousdev Jan 07 '17 at 02:46
  • Possible duplicate of [JQuery - $ is not defined](https://stackoverflow.com/questions/2194992/jquery-is-not-defined) – Hidden Hobbes Jun 04 '17 at 17:09

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
  • 625
  • 1
  • 6
  • 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