Add javascript to a content type form in Drupal 7 I'm trying to add a custom javascript file to my article content type forms (add/edit).

I've tried mythemename_preprocess_page, hook_form_alter and  theme_content_type_node_form to no avail. They both seem to pick up the page i'm on, not the form in Drupal's Overlay with the form in it.

Obviously I want the Javascript to fire only when I'm actually on the form. So just putting the Javascript in a onReady function in a sitewide js file won't suffice.

My current setup in template.php:

function zeilschepen_form_alter(&amp;$form, &amp;$form_state, $form_id){
  switch ($form_id) {
    case 'node-accommodation-form':
      $form['#after_build'][] = 'zeilschepen_after_build';
      break;

    case 'views_exposed_form':
      if (isset($form['field_country_value'])) {
        $form['field_country_value']['#attributes'] = array('placeholder' =&gt; array(t('Land...')));
      }
      break;
  }

}

function zeilschepen_after_build($form, &amp;$form_state) {
  $theme_path = drupal_get_path('theme', variable_get('theme_default', NULL));
  drupal_add_js ("$theme_path/js/test.js");
  return $form;
}


And test.js:

(function ($, Drupal, window, document, undefined) {

    Drupal.behaviors.test = {
        alert('test');
    }
})(jQuery, Drupal, this, this.document);
This:

node-accommodation-form


Is the HTML ID of the form, found in $form['#id'] (confusing I know). You need the internal ID, which is

node_accommodation_form


As in:

function zeilschepen_form_alter(&amp;$form, &amp;$form_state, $form_id){
  switch ($form_id) {
    case 'node_accommodation_form':


Using #attached, as mentioned in comments, is far preferable to drupal_add_js() when you get to that part.

As an aside, you may find coming back to your code later on a bit easier if you separate out your form alters like so:

function MYMODULE_form_node_accommodation_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  // Logic specific to the accommodation node form, no need to check form ID.
}


Also, as noted in another answer, theme hooks are only run when the theme is actually in use. So if you're using the admin theme for your node edit forms, your code will never run. If that's the case, you need to move the logic to a module.