Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can jQuery and Mootools work together?

Can jQuery and Mootools work together??

If not when is that case?

like image 919
eomeroff Avatar asked Jul 28 '10 12:07

eomeroff


3 Answers

There's more to it than just noConflict.

jQuery is an intrusive library. It adds an internal jQuery123 (for some randomised per-instance value of 123) property to every element it touches (which is anything with data or event handlers, amongst other reasons). In IE, this property also gets reflected as an attribute.

So if MooTools or any other library (or indeed, a plain DOM method) comes along and starts messing with those properties/attributes, or cloning elements, or hacking innerHTML, they're likely to mess up these supposedly-unique identifiers, causing jQuery to get confused and start misbehaving in ways it is extraordinarily difficult to debug.

jQuery also fiddles a bunch of event code to try to make submit/focus/blur/focusin/focusout/mouseenter/mouseleave events work and bubble across browsers. This may confuse other-library code that is not expecting it.

So, with jQuery 1.4, you can just about get away with using another library at the same time, as long as they are working on separate elements that don't interact with each other. (jQuery 1.3 was also much more promiscuous about what elements it ‘touched’.)

But in general I would not recommend two major frameworks on one page.

like image 59
bobince Avatar answered Nov 05 '22 09:11

bobince


jQuery can be used in no-conflict mode:

jQuery.noConflict();

or could use jQuery instead of $.

jQuery('#myelement').hide();

As well in MooTools there's a document.id() method that could be used instead of $:

document.id('myelement');

In case you want to be able to use a $ you can try the snippet below:

(function($) {

   $('#myelement').click(function() {
       ...
   });


})(jQuery);

By the same way you can use $ from MooTools

like image 25
fantactuka Avatar answered Nov 05 '22 08:11

fantactuka


Use dollar safe mode in Mootools and you should be ok as jQuery does not extend natives.

like image 1
Nic Bell Avatar answered Nov 05 '22 07:11

Nic Bell