Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cant Dollar symbol be used as an alternative fo jQuery in Wordpress?

Tags:

wordpress

Why can't we use "$" instead if jQuery in wordpress? Usually "$" is the alternative of jQuery right? But in Wordpress it shows "$" is not a function. I dont get why is that so. Can anyone explain?

like image 426
Midhun Mathew Avatar asked Feb 28 '13 10:02

Midhun Mathew


People also ask

Can we replace dollar sign in jQuery?

noConflict() "frees" the "$" from being associated with jQuery. Normally in your code you can use $ as a replacement for "jQuery". If you use noConflict() you can't do that anymore and so you have to replace each "$" with "jQuery"; .

Why is jQuery a dollar sign?

The $ sign is nothing but an identifier of jQuery() function. Instead of writing jQuery we simply write $ which is the same as jQuery() function. A $ with a selector specifies that it is a jQuery selector. It is given a shorter identifier as $ just to reduce the time for writing larger syntax.

Can we use any other symbols instead of in jQuery?

In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $ . If you need to use another JavaScript library alongside jQuery, return control of $ back to the other library with a call to $. noConflict() .

Can jQuery be used in WordPress?

WordPress comes bundled with jQuery and some essential jQuery libraries. WordPress theme and plugin developers can easily call jQuery in their own plugins and themes to add their own jQuery scripts. To call jQuery in WordPress theme or plugin, users need to add their own jQuery scripts and enqueue them in WordPress.


2 Answers

Have a look at tip 5 in this web page: 5 Tips for using jquery with wordpress:

It is important to know that the version of jQuery that comes with WordPress automatically calls the jQuery.noConflict(); function, which gives control of the $ variable back to whichever library first implemented it. If you are loading a different copy of jQuery, you'll need to manually call jQuery.noConflict();, if necessary, from one of your JavaScript files.

I hope that helps.

like image 57
Matt Hogan-Jones Avatar answered Sep 23 '22 12:09

Matt Hogan-Jones


The tricky thing is this particular copy of jQuery is in compatibility mode by default. That means that the typical ‘$’ shortcut for jQuery doesn’t work, so it doesn’t conflict with any other JavaScript libraries that use the dollar sign also, like MooTools or Prototype.

Source: Using ‘$’ instead of ‘jQuery’ in WordPress

like image 43
zengr Avatar answered Sep 21 '22 12:09

zengr