I am using a multistep form in Drupal 6.x. But in each step or even in same step on resubmissions I get different form ids and different element ids.

The first time I get something like 'edit-xxx-1' but subsequent times I get 'edit-xxx' without the number. Because of this I am not able to access the form elements with javascript, jscript as I need same element id .

Is there a way to pass on the form id to subsequent requests ?

Karthik

eg.

<input type="text" maxlength="10" name="end_date" id="edit-end-date-1" size="10" value="01/01/2030"  class="form-text" />
<input type="submit" name="op" id="edit-submit-1" value="Delete"  class="form-submit" />
<input type="hidden" name="form_build_id" id="form-47126414ffa9ca562649c147be56f8d5" value="form-47126414ffa9ca562649c147be56f8d5"  />
<input type="hidden" name="form_token" id="edit-alogbook-formz-form-token-1" value="73eed4b9915bdc16f86cbb497c41b48b"  />
<input type="hidden" name="form_id" id="edit-alogbook-formz-1" value="alogbook_formz"  />

But when I resubmit the form I get different values for the form element ids :

<input type="text" maxlength="10" name="end_date" id="edit-end-date" size="10" value="01/01/2030"  class="form-text" />
<input type="hidden" name="form_build_id" id="form-6149df05f231c641a14ed5e91055c9a2" value="form-6149df05f231c641a14ed5e91055c9a2"  />
<input type="hidden" name="form_token" id="edit-alogbook-formz-form-token" value="73eed4b9915bdc16f86cbb497c41b48b"  />
<input type="hidden" name="form_id" id="edit-alogbook-formz" value="alogbook_formz"  />
<input type="submit" name="op" id="edit-submit" value="Delete"  class="form-submit" />

Comments

jefkin’s picture

Well, I'm going to suggest the best way is with your own module code.

Then I think the best thing to do is give your element a class, (for argument sake, 'my-element'), with a hook_form(), or hook_form_alter() depending on your use case like one of these:

my_module.module in function hook_form:

$form['my_element']['#attributes']['class'] = 'my-element';

or my_module.module in function hook_form_alter:

if (is_array($form['my_element']))
{
  $element = $form['my_element'];

  if (!array_key_exists('#attributes', $element))
  {
    $element['#attributes'] = array ( );
  }
  if (is_array($element['#attributes']))
  {
    $attributes = $element['#attributes'];

    if (!array_key_exists('class', $attributes))
    {
      $attributes['class'] = '';
    }
    $attributes['class']   .= ' my-element';
    $attributes['class']    = trim($attributes['class']);
    $element['#attributes'] = $attributes;
  }
  $form['my_element'] = $element;
}

my_module.js

var MySpace = (("undefined" == typeof MySpace) || (!MySpace)) ? {} : MySpace;

MySpace.MyModule = function ()
{
  /*
  ** private attributes
  */
  var sample = null
    ;
  /*
  ** private methods
  */
  var hidden = function () 
  {
  };

  var that = 
    /*
    ** publicly accessible components
    */
    { samplePublic: []

      /*
      ** public method MySpace.MyModule.myCoolFunction() - coolness
      */
    , myCoolFunction = function ()
      {
        sample = 'saved';
        hidden();
        alert('A click happened');
      }
    };

  return that;
}();

Drupal.behaviors.my_module = function (context)
{
  // Attach my Cool Function
  //
  $('.my-element:not(.my_module-processed)', context).
                    bind('click', MySpace.MyModule.MyCoolFunction).
                    addClass('my_module-processed');
};

In the worst case, if none of the above would work for you (or maybe the js / behaviors scare you) you could simply override your element's default 'id' using the '#id' FAPI element, I did this before I was comfortable with Drupal behaviors when my javascript had to grab a specific element.

Good luck