views:

3788

answers:

3

I've got the following situation

  • A rails application that makes use of rjs / Scriptaculous to offer AJAX functionality
  • Lot of nice javascript written using JQuery (for a separate application)

I want to combine the two and use my JQuery based functionality in my Rails application, but I'm worried about JQuery and Scriptaculous clashing (they both define the $() function, etc).

What is my easiest option to bring the two together? Thanks!

+2  A: 

jRails is a drop-in replacement for scriptaculous/prototype in Rails using the jQuery library, it does exactly what you're looking for.

jpeacock
+13  A: 
jQuery.noConflict();

Then use jQuery instead of $ to refer to jQuery. e.g.,

jQuery('div.foo').doSomething()

If you need to adapt jQuery code that uses $, you can surround it with this:

(function($) {
...your code here...
})(jQuery);
noah
+2  A: 

I believe it's jQuery.noConflict().

You can call it standalone like this:

jQuery.noConflict();

jQuery('div').hide();

Or you can assign it to another variable of your choosing:

var $j = jQuery.noConflict();

$j('div').hide();

Or you can keep using jQuery's $ function inside a blocked like this:

jQuery.noConflict();

 // Put all your code in your document ready area
 jQuery(document).ready(function($){
   // Do jQuery stuff using $
   $("div").hide();
 });
// Use Prototype with $(...), etc.
$('someid').hide();

http://docs.jquery.com/Using_jQuery_with_Other_Libraries

Chris MacDonald