Kyle
Kyle

Reputation: 35

How to give the $ sign back to jQuery?

I am already declaring jQuery at the top of my <head>, below that I am referencing a ton of other plugins. When I do this in the body of my page:

<script type="text/javascript">
    $(function() {
        alert('hello');
    });
</script>

I don't get the alert. However if I redeclare jQuery again like so:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

<script type="text/javascript">
    $(function() {
        alert('hello'); 
    });
</script>

It works. Is there any way to reassign the $ back to jQuery (which is what I'm guessing is the problem here). So I can use $ instead of jQuery noConlict.

Upvotes: 0

Views: 259

Answers (4)

jfriend00
jfriend00

Reputation: 708156

You can also just do this if you want $ returned to be jQuery globally:

window.$ = jQuery;

Upvotes: 1

natedavisolds
natedavisolds

Reputation: 4295

Encapsulate like this:

<script type="text/javascript">
   (function($) {
      alert('hello');
   })(jQuery);
</script>

Upvotes: 1

DA.
DA.

Reputation: 40697

You are placing your jQuery inline on the page rather than in a document ready block. If you don't have them in a document ready block, there's no guarantee that jQuery has fully loaded before the function is fired.

Upvotes: 1

Naftali
Naftali

Reputation: 146360

<script type="text/javascript">
   jQuery(function($) {

      alert('hello');
      //use $ however u want

   });
</script>

Fiddle (using $_): http://jsfiddle.net/maniator/B3hU4/

Upvotes: 1

Related Questions