How can I change the ID of a hidden element (created with the Drupal 6 webform module) using hook_form_alter()?

The ID that it outputs is something like '#edit-submitted-component-name'. I need the ID to be something very custom and I need to avoid changing it with Javascript. The form API does not support the #atrributes token on hidden elements, so I can't set the ID via hook_form_alter();

Any suggestions would be really helpful. Thank You.

1 Answer 1


I had a similar issue to deal with. I need to generate form IDs programmatically so I implemented below code.

function voci_comment_form($form, &$form_state, $cid) {
     $form['#attributes']['id'] = 'voci-comment-form-' . $cid;
     $form['#attributes']['class'][] = 'voci-comment-form';
     $form['body'] = array(
      '#title' => 'Post a comment',
      '#type' => 'textarea',
      '#resizable' => FALSE,
      '#rows' => 1,
     $form['comment'] = array(
      '#type' => 'submit',
      '#value' => 'Comment',

      return $form;

It's true that you can set $element['#attributes']['id'] and that will apply to the form field.

I hope this helps... :)

  • Thanks for your answer. I want to set the ID attribute to a hidden element without affecting the form or any other element (<input type="hidden" id="custom-id" />). I checked the Drupal Form Api and $element['#attributes']['id'] is not supported for hidden elements. Jun 21, 2016 at 9:26

Your Answer

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

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