Solved

How to use JQuery functionality in WordPress?

Posted on 2014-03-18
6
297 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
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 is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

773 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