2

I found that there is a way to add javascript files in the form validation function as stated in "drupal_add_js not work in form validation failed".

However, I would like to know how to add javascript using the drupal_add_js function with the inline option. I would just like to execute some javascript code while I am in the validation function of the Drupal Form API.

3

See the docs @https://www.drupal.org/docs/7/api/javascript-api/managing-javascript-in-drupal-7

drupal_add_js() example:

drupal_add_js(
  'jQuery(document).ready(function () { alert("Hello!"); });',
  array(
    'type' => 'inline',
    'scope' => 'footer',
    'group' => JS_THEME,
    'weight' => 5,
  )
);

#attached example:

$element['#attached']['js'][] = array(
  'type' => 'inline',
  'data' => 'jQuery(document).ready(function () { alert("Hello!"); });',
  'scope' => 'footer',
  'group' => JS_THEME,
  'weight' => 5,
);

Just replace $element by $form.


Original source: https://web.archive.org/web/20160502205421/http://www.failover.co/snippets/drupal-7-attaching-css-and-javascript-forms-or-form-elements

| improve this answer | |
1

Use the #attached property to add your JS file instead of drupal_add_js function

More info: FAPI #attached

| improve this answer | |
  • I am thinking that this is just what I will do, but I meant to ask if it is possible to use the inline option of the drupal_add_js function in this situation. – Guybrush Threepwood Sep 28 '15 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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