Thursday, September 1, 2011

D6 - SIMPLE module to get JavaScript working on a form

The very quick and easy way to get some JavaScript working on your form:

  1. Add the 3 files below to your basic module structure.  This module is called "your_module" for example purposes.
  2. Enable the module
  3. Clear cache

your_module.info:
name = Demo Information
description = Custom handling of demo information
core = 6.x
package = Your Company


your_module.module:
<?php

/**
 * Implementation of hook_form_alter().
 */
function your_module_form_alter(&$form, $form_state, $form_id) {
  // Only include on WHATEVER node add/edit forms.  Adjust these lines based on your content types.
  if ($form_id == 'new_item_node_form') {
    drupal_add_js(drupal_get_path('module', 'your_module') .'/your_module.js');
  }
}


your_module.js:
alert("Gotcha!");



Look at the HTML source and you should see your_module.js listed within all the other JavaScript files.

No comments:

Post a Comment