yarek
yarek

Reputation: 12054

jquery: $ is undefined

I run a wordpress blog. It includes Jquery

<script type='text/javascript' src='/wp-includes/js/jquery/jquery.js?ver=1.12.3'></script>
<script type='text/javascript' src=/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.0'></script>

When I run jQuery I got:

function (a,b){return new n.fn.init(a,b)}

when I try

$("div") 

I got

Uncaught TypeError: $ is not a function(…)

when I try jQuery("div")

I got:

a.fn.init[203]

How can I use Jquery ?

Upvotes: 1

Views: 3987

Answers (3)

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

It looks like jQuery is not bound to the $ variable (to avoid conflicts, probably).

If you want to use the $ variable then you can create a scope wherein it will be bound to that variable:

(function ($) {
    // Use $ in this scope
}(jQuery));

If all your code should run when the document is ready (which is usually how it is done) then you can also use the scope that jQuery creates for you:

jQuery(function ($) {
    // Document ready, use $ in this scope...
});

You can also just add jQuery to a global $ variable. If you know for certain that only jQuery will be using that variable then it is probably fine.

$ = jQuery;

Upvotes: 0

h2ooooooo
h2ooooooo

Reputation: 39542

As others have told you, this is due to jQuery.noConflict. You can use a Immediately-Invoked Function Expression to map the $ variable to jQuery and simply place your logic inside of the expression:

(function($) {
    // $ is now the same as jQuery

    $('body').remove();
})(jQuery);
// ^^^^^^ we're passing this to the "function($)" call

Upvotes: 0

Vincent
Vincent

Reputation: 1044

$ is an alias of the jQuery() function, which apparently missing in this case...

One way is to do, between jQuery and your other scripts:

<script type="text/javascript">
    var $ = jQuery;
</script>

It should work

Upvotes: 4

Related Questions