Ian Hoar
Ian Hoar

Reputation: 1184

Running Multiple versions of jQuery at the same time

I need to run two versions of jQuery. I've tried aliasing j to jQuery. Here is my code.

j = jQuery.noConflict(); // No conflict with older version of jQuery
(function($) {
  $('#any_id_or_class').on("click", function(event){
    console.log('clicked');
  });
})(j);

I've logged the version of jQuery using $.fn.jquery and I get two distinct versions depending if I log from inside my function or outside the scope. If I put a click to the 'body' of the document $('body') I get output, but here's where it all goes wrong, if I use any class or id on the page nothing happens? Ideas?

Edit 1: Obviously I would like to use only one version of jQuery, but that isn't an option.

Edit 2: Here's how I got it working, not ideal, as I wanted to try and alias the $ within the scope of the function.

j = jQuery.noConflict(); // No conflict with older version of jQuery

j(document).ready(function(){
  j('#masthead').on("click", function(event){
    console.log('clicked');
  });
});

Upvotes: 0

Views: 1757

Answers (1)

RTulley
RTulley

Reputation: 175

var j = jQuery.noConflict(); // Unbinds the last included version of Jquery

  j('#any_id_or_class').on("click", function(event){
    console.log('clicked');
  });

The noConflict assignment will de-register the last included jquery file, so make sure the last included one is the new version to work with .on

Upvotes: 1

Related Questions