Solved

How to use JQuery functionality in WordPress?

Posted on 2014-03-18
6
296 Views
Last Modified: 2014-03-18
Hi experts,
We are trying to make use of the JQuery that gets automatically loaded by WordPress but our attempts to use the $ alias fails anywhere outside of the document.ready() function. Below are the main points:

WordPress loads JQuery in its template header file. The jquery.js file that WordPress includes contains jQuery.noConflict(); at the very end of it.

In our document.ready() function, we are able to use $ but ONLY within the document.ready() function itself.

However, we make a call to a regular javascript function that is contained in another javascript file and that other function tries to use $ but it fails to be recognized.

Below is a basic

<script type=text/javascript>
jQuery(document).ready(function($)
{
    $(".thisworksfine").hide();
    
	// call a function that tries to use JQuery $
	TurnOffMouseWheel();
}); 
</script>

Open in new window


The function TurnOffMouseWheel() is contained in another js file we include (called commonfunctions.js). Below is the code for the TurnOffMouseWheel() function contained in commonfunctions.js:

function TurnOffMouseWheel()
{
	$("select").each(function(){
				$(this).bind("mousewheel", function(e){
      				return false;
    			}); // bind
			}); // each
}

Open in new window


We've tried googling a solution and thought we had followed the correct steps but it's still not working.

Are we missing something basic?

thanks experts!
0
Comment
Question by:rascal
  • 3
  • 3
6 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39938155
In WP use jQuery not $ - its to avoid conflicts with other libraries
0
 
LVL 1

Author Comment

by:rascal
ID: 39938178
Thanks Gary,
But there are tons of articles that say that in fact we CAN use the $.

For example:
http://matthewruddy.com/using-jquery-with-wordpress/

We would much rather make a small tweak in order to use the $ instead of having to convert all our jquery.

Any chance we can get the $ working?
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39938205
You can pass $ into the function but you still need to declare its a jQuery function as they describe in the Using No Conflict Mode section.
So you are still going to have to go through all the functions.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:rascal
ID: 39938239
Thanks Gary,
We were confused a bit by the documentation regarding passing $ to the function.

Our function declaration in commonfunctions looks like this:

function TurnOffMouseWheel()

and we call it simply by coding: TurnOffMouseWheel().

Would you mind illustrating how we actually pass $ to the function?
0
 
LVL 58

Expert Comment

by:Gary
ID: 39938274
This works as you know since you are passing $ into the function after declaring a jQuery function
<script type=text/javascript>
jQuery(document).ready(function($)
{
    $(".thisworksfine").hide();
    
	// call a function that tries to use JQuery $
	TurnOffMouseWheel();
}); 
</script> 

Open in new window


To pass it along to another function you need to pass it in the function call in the code above
TurnOffMouseWheel($);

And then your function becomes.
function TurnOffMouseWheel($)
{
	$("select").each(function(){
				$(this).bind("mousewheel", function(e){
      				return false;
    			}); // bind
			}); // each
} 

Open in new window

0
 
LVL 1

Author Closing Comment

by:rascal
ID: 39938575
Thanks Gary!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Replace &lt; with < 14 56
How to avoid jquery conflicts in Wordpress ? 4 24
Access Object's Property 9 22
Intermittent Error on Page Loading 4 20
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now