On my Drupal site, I have a simple jquery file attached to the theme. This file is added in preprocess_node. The whole content of the file is as follows:
(function ($) {
console.log("hello world");
$('li').addClass('hello');
$('div').addClass('hi');
})(jQuery);
Here is what happens when I run any Drupal page:
- no javascript errors
- the console shows "hello world "
- in Chrome debug, div's do not have class "hi" and li's do not have class "hello"
I ran the same (without wrapper function) on jsfiddle for some basic html and the result was as expected, classes were added to div's and li's and can be seen in Chrome debug. So why is it not working in Drupal?
$(document).ready()
to make the code wait until the DOM is ready. jsFiddle defaults to only running your javascript code after the page has loaded which could explain why it works in jsFiddle.