Search code examples
prototypejsjqueryjslider

jQuery conflict while using jslider with prototype and noConflict


Trying to resolve $ conflict between prototype.js and jQuery.js because I need to use jSlider which requires the jQuery library.

I add jQuery.noConflict() at the end of the jquery.js file then in the jslider.js file I change $._roundNumber to jQuery._roundNumber. Yet I still get an error which says jQuery._roundNumber is not a function. Please help.


Solution

  • Fixed. Just realised that the reason jQuery.noConflict() was not working was because jQuery was already being used in the template page that I was including in my current page so the problem was two fold:

    1. '$()' which is the default jQuery selector was clashing with prototype.js which Tapestry includes by default

    2. when I added jQuery.noConflict() at the end of my jquery.new.js file then tried using 'jQuery()' as the selector it did not work either because a different version of jquery (let's call this jquery.old.js) was already included in the template page (which I was including in my current page). To get the jquery.old.js to work with prototype.js, the previous developer already used jQuery.noConflict() so the 'jQuery()' selector was already taken as well.

    Fix: I added $j2 = jQuery.noConflict() at the end of jquery.new.js file then I used '$j2()' selector in my new library that uses the jSlider and I also modified the selector in the jSlider.js file. It all works well now.

    I am leaving both jquery.new.js and jquery.old.js file in the project (rather than using just one) because the template page which uses the older jquery version is used all over the site (I don't want to test the entire site after changing this) while the new component I'm adding needs the new version of jquery (jquery.new.js) file. And yes I know it's better to add the no conflict commands at the top of my html page rather than at the end of the js library files - will change that after this update on stack overflow :)