Solved

How to use JQuery functionality in WordPress?

Posted on 2014-03-18
6
301 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 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
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 Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

732 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