I am trying to put code into a block that contains a Javascript onclick handler. Have been trying to figure out how to use drupal_add_js, but cannot figure out how to make an onclick function work. I believe I may need to use Drupal behaviors.

I have read the documentation, but it is not clear. Here is what I am trying but doesn't work. Any help would be appreciated.

drupal_add_js('jQuery(document).ready(function() { $("#trial").click(function() { alert("Handler called." ); } ); } );', 'inline');

1 Answer 1


$ isn't defined in the global scope...you either need to use a closure:

(function($) { 
  // Use $ safely here.

or use jQuery every time


If you check the console you should see an error in there to the effect "$ is undefined".

Your Answer

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

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