web design and coding services with a huge twist of creativity.

Stuff Tagged ‘tip’

Quick Tip: Using jQuery.noConflict()

Published 1 year ago, in Blog, Web

If you’re using jQuery along with another Javascript Framework (such as Prototype) which also implements the $ variable, you can give back control over it to whichever library first implemented it by using the following code (right after including jQuery):

jQuery.noConflict();

Now, instead of using $ to select jQuery objects, you will be able to use jQuery:

var el = jQuery("#myID");

However, typing jQuery every single time you want to select something gets old real quick, so you can use this trick when declaring noConflict():

var $j = jQuery.noConflict();

This reassigns the reference to the jQuery object to whichever variable you want. In this example, you can now use $j instead of jQuery as a selector. You’ll be (at least) 10 times more productive now!