Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught ReferenceError: $ is not defined when using $(document).ready

I'm using the Foundation CSS framework, which loads jQuery in the footer. This is not atypical and recommended practice by many. However, I need to write scripts in the page. Using document ready should do the trick, but I'm still getting an error. Why?

$(document).ready(function(){
    // My Code
    var a;
    $(#id).dosomething({
    // Doing Something
    });
});

Just before body close tag:

<script src="scripts/jquery-1.8.0.min.js"></script>
// More Scripts
</body>

But I get the standard error "Uncaught ReferenceError: $ is not defined" (4x because I have 4 scripts in the body). Also, I cannot move the jQuery reference above the script call as it conflicts with other scripts. I cannot move the script call below jQuery reference either.

like image 418
roadsunknown Avatar asked Jul 24 '26 16:07

roadsunknown


2 Answers

Using document ready should do the trick, but I'm still getting an error. Why?

I think you misunderstood. You use $(document).ready to register code that should be executed once the DOM is read, which is typically when you want to manipulate the DOM. $ is jQuery here, so obviously it has to be loaded before you call $(document)....

I cannot move the jQuery reference above the script call as it conflicts with other scripts

Maybe you should use jQuery.noConflict() [docs] then.

like image 160
Felix Kling Avatar answered Jul 26 '26 04:07

Felix Kling


It's a bit old, but maybe it will help other people. I got the same error and it was because I loaded my own JS script before the Jquery CDN in the html page. You must call the JQuery CDN before calling your own script. <script src = "https: // jquery-link / .../jquery.js" /> and after <script src = "/ myfoder / myscript.js />

like image 38
Sebastien Avatar answered Jul 26 '26 06:07

Sebastien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!