I need to write some custom js lines into my drupal site, but whatever i do, my drupal site won't
i have implemented the files using this line into my .info file

"scripts[] = js/myscript.js"

the code is

(function ($) {
$('th:contains("sometext")').addClass('some-class');
})(jQuery);

When looking at the source code on the side.. everything seems ok. The file is implemented.

But whatever i do, it does not work.
I tried catching several times
Impementet alle the code in http://jsfiddle.net/, it worked.
Tried difference way to implement the code.
The file path and everything is 100% correct.
Been struggling with this for 5 hours, and i cant figure out what im doing wrong.
This is so frustrating, what am I missing here?

Comments

VM’s picture

according to Using jQuery on https://www.drupal.org/docs/7/api/javascript-api/managing-javascript-in-...

(function ($) {
  // All your code here
}(jQuery));

Looks like your snippet above has a paren in the wrong place. Also, because you adding this to the theme .info file be sure to clear your drupal cache.

John_B’s picture

This is true.
However it dose work both ways, though I am not sure why. I tried this on a test site and found that the problem is that the jQuery function is firing too early, before the HTML has loaded. Look at point 2) here for the (document).ready function you need to wrap your function to prevent this problem.

Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors

Jaypan’s picture

John is right. You need to use Drupal.behaviors.

Jaypan’s picture

>Looks like your snippet above has a paren in the wrong place. Also, because you adding this to the theme .info file be sure to clear your drupal cache.

The parenthesis is correct actually. Both will work, but jSlint recomments the version shown above (which I personally use) with this explanation:

Wrap an immediate function invocation in parentheses to assist the reader in understanding that the expression is the result of a function, and not the function itself