Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI and Prototype conflict

I am adding a new form to our website in Perl (wasn't my choice). There is a lot of html generate automatically for the forms to create the consistant look. My problem comes in where the legacies use prototype for various things throughout the page (including on load). However i want to use jQuery and mainly the date picker from jQuery UI. I can avoid conflicts by using jQuery.noConflict();. But i still get an error becuase of a line of code in the jQuery date picker javascript.

inst.dpDiv.zIndex($(input).zIndex()+1);

See how it still contains the $ symbol, which prototype tries to handle but can't. Does anyone have any solutions for me? I am using jQuery 1.5 and jQuery UI 1.8.6.

In summary: I can't remove prototype.js, I would prefer to use jQuery UI datepicker and jQuery UI datepicker doesn't handle jQuery.noConflict().

Thanks

EDIT This only happens when i try to click on the button to show the date picker. From firebug:

$(input).zIndex is not a function
inst.dpDiv.zIndex($(input).zIndex()+1);
datepicker.js (line 651)

EDIT EDIT Updating to jQuery UI 1.8.9 doesn't change the problem.

EDIT EDIT EDIT This problem occurs when clicking the button to show the date picker. So on load the datepicker is set up fine. Changing the order of the scripts doesn't work, and any sort of variation of the following code doesn't work either.

(function($) { 
  $(function() {
    // more code using $ as alias to jQuery
  });
})(jQuery);
like image 256
Timothy Ruhle Avatar asked Mar 20 '26 04:03

Timothy Ruhle


2 Answers

<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    jQuery.noConflict();
    jQuery(function ($) {
        // your code here
        // $ is the jQuery object
    });
</script>

jQuery will pass itself to the inner function. Just specify $ as the parameter and $ inside the function will refer to the jQuery object. The jQuery UI datepicker does the same. Make sure you include the libraries in the above order, Prototype first.

like image 160
deceze Avatar answered Mar 22 '26 17:03

deceze


I dont think there are any elegant solutions. I think you have two options - and they both involve search/replace which shouldn't be a big deal if you're proficient with perl.

  1. replace the $-function with jquery() in the datepicker library and add a jQuery.noConflict()

  2. replace the $-function in prototype with $$$ (or another name) and all it's instances

The second option may be your best bet if you are in the process of removing prototype. It is less obtrusive - but then again it depends on the amount of code you have in prototype. I'd be interested to see if anyone has any other ideas.

like image 22
stiggs Avatar answered Mar 22 '26 17:03

stiggs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!