views:

27

answers:

3

My prototype code no longer works when I load in the jquery library, even though jquery is the first library to be loaded.

The html of the page looks a bit like so:

<head>
...
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/effects.js"></script>
<script type="text/javascript" src="/js/prototip.js"></script>
<script type="text/javascript" src="/js/dragdrop.js"></script>
<script type="text/javascript" src="/js/default.js"></script>
<script type="text/javascript" src="/js/limitfocus.js"></script>
...
</head>

<body>

<input type="radio" name="useOtherAddress" id="useOtherAddress_y" value="y" onclick="selectAddress()">
<input type="radio" name="useOtherAddress" id="useOtherAddress_n" value="y" onclick="selectAddress()">

<div id="myAddress" style="display: none;">
...
</div>

<script type="text/javascript">
  // the piece of protptype code no longer working 
  function selectAddress () {
    var t = $('myAddress');
    if ($('useOtherAddress_n').checked) {
      Effect.Appear(t);
      return;
    }
    t.hide();
  }
</script>

Anybody have any ideas? This page:

http://docs.jquery.com/Using_jQuery_with_Other_Libraries

suggests that loading jquery first should make everything handy-dandy. But it's not :[

+4  A: 

You need to call jQuery.noConflict(), like this:

jQuery.noConflict(); //or possibly $j = jQuery.noConflict();
function selectAddress () {

Then load jQuery last (but before it's plugins) so that the jQuery.noConflict() will restore the $ to its previous value (Prototype).

Nick Craver
I think the OP know's that and he means [this](http://docs.jquery.com/Using_jQuery_with_Other_Libraries#Including_jQuery_before_Other_Libraries) - where it says `There is no need for overriding the $-function by calling "jQuery.noConflict()".` - I myself is confused with the docs too.
Reigel
@Reigel -That only holds true if the library you're including afterwards doesn't do an `if(typeof $ == "undefined")` type check, that documentation is shaky at best, it's an assumption, and not always a correct one.
Nick Craver
I now wished the docs were clear about this. ;) thanks.
Reigel
@Reigel You are right, that is exactly what I mean.
superspace
@Nick Will try that out and see how I go
superspace
This worked. Thanks
superspace
+2  A: 

If you can help it at all - the code you show doesn't display any compelling need to use both libraries at once - as a general policy, using jQuery and Prototype alongside should be the last resort or a temporary workaround, for example while switching frameworks.

Even if you get the frameworks running together, there are dozens of subtle pitfalls in DOM handling and such that may bite you in the arse later, creating untraceable bugs.

It's really, really best to use one JS framework only.

Pekka
`It's really, really best to use one JS framework only.` +1 for this.
Reigel
@Pekka that's because I haven't added the jquery code in yet :) I'm just loading in the jquery library at this stage.
superspace
superspace
@superspace that may work okay. I'm strongly against habitually using both frameworks together, but as long as Protoype and jQUery don't manipulate the same DOM elements or stuff like that, chances are everything works out well.
Pekka
+2  A: 

+1 as a general policy, using jQuery and Prototype alongside should be the last resort or a temporary workaround, for example while switching frameworks.

My guess is that you are using jQuery with the $ variable or using a function that will cause conflicting!

when ever you want to use jQuery use jQuery instead of $ like jQuery(#id).click();. in jQuery $ is just an alias for jQuery, so all functionality is available without using $. If we need to use prtotype alongside with jQuery, you can return control of $ back to prototype with a call to $.noConflict() if you wanna use $ for both you can do that. here's how:

...

<script type="text/javascript" src="/js/effects.js"></script>
<script type="text/javascript" src="/js/prototip.js"></script>
<script type="text/javascript" src="/js/dragdrop.js"></script>
<script type="text/javascript" src="/js/default.js"></script>
<script type="text/javascript" src="/js/limitfocus.js"></script>

<script type="text/javascript" src="/js/jquery.min.js"></script>
...
</head>

<body>

<input type="radio" name="useOtherAddress" id="useOtherAddress_y" value="y" onclick="selectAddress()">
<input type="radio" name="useOtherAddress" id="useOtherAddress_n" value="y" onclick="selectAddress()">

<div id="myAddress" style="display: none;">
...
</div>

<script type="text/javascript">
  // the piece of protptype code no longer working 
$.noConflict();
  jQuery(document).ready(function($) {
    //jquery with $

  });
//prototype with $
  function selectAddress () {
    var t = $('myAddress');
    if ($('useOtherAddress_n').checked) {
      Effect.Appear(t);
      return;
    }
    t.hide();
  }
</script>
Neo
Also if you can tell me what error you are getting, I think it will really help!
Neo
With my current setup, with firebug turned on, and clicking on the radio buttons, i get zilch. Nothing. No errors. It just doesn't work.
superspace
@Neo,thanks for your answer. It is also correct and it's an extension of the answer by Nick. Answer has been marked as useful
superspace