Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to use JQuery functionality in WordPress?

Posted on 2014-03-18
6
Medium Priority
?
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
The purpose of this video is to demonstrate how to properly insert a Vimeo Video into a WordPress site or Blog. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp…
This video teaches users how to migrate an existing Wordpress website to a new domain.

610 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