1

I added a javascript file to my site in the theme's .info file

scripts[] = js/myjs.js

The js file loads and I can see that it's included in the head when I load the site, however the jQuery javascript doesn't work at all. I have a simple show/hide when a div on the page is clicked by it's not working.

This works fine when it is not in the Drupal site.

By clicking "Click here to start quiz" it should hide that div and show another.

I have a feeling it is because I do not have the most updated version of jQuery installed (I have 1.4.4 instead of 1.9.1), but I'm not sure how to update it for the theme I'm using.

Any ideas?

3
  • I was able to solve the problem by changing the document.ready function on the first line of my js file from: $(document).ready(function () { to: jQuery(document).ready(function ($) { I found the answer here: stackoverflow.com/questions/10807200/…
    – CR47
    Commented Apr 7, 2013 at 21:30
  • 2
    not to be a stickler, but ideally you should post this as an answer to your question rather than a comment, that way your question will be "Closed" and won't show up in the Unanswered category. cheers Commented Apr 8, 2013 at 1:16
  • Unfortunately stackexchange will not let me answer my own questions within 8 hours of posting, and even suggests I post as a comment instead. I was planning on updating this later as an answer to the question.
    – CR47
    Commented Apr 8, 2013 at 10:44

1 Answer 1

0

I was able to solve the problem by changing the document.ready function on the first line of my js file from: $(document).ready(function () { to: jQuery(document).ready(function ($) { I found the answer here:

4
  • Also ensuring jQuery update module is installed does not hurt either
    – user12342
    Commented Apr 8, 2013 at 13:25
  • @CR47 one other thing you might want to check out is the drupal examples module, which includes a JS example. In that example's js files, you can see they format jQuery code like so, which worked for me every time: (function($){ $(document).ready(function(){ .... script goes here }); })(jQuery); Commented Apr 9, 2013 at 4:32
  • @Cromian yes I had had that enabled and updated as well, though that was not the problem.
    – CR47
    Commented Apr 14, 2013 at 18:12
  • AFAIK you should be able to put substitute $ for jQuery anywhere in the code without a problem, as long as you have "(jQuery);" at the end of your JS file like in the example in my earlier comment Commented Apr 14, 2013 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.