0

I'm trying to add an additional class to a form element using the following code:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function grant_opportunity_form_grant_opportunity_node_form_alter(&$form, &$form_state, $form_id) {

    $form['grant_opportunity_end_date']['#attributes']['class'][] = 'datepicker';   

}

But the additional class is not being added. Am I doing this incorrectly? This is a very simple thing I'm trying to do, so I'd prefer not to go through the trouble of overriding the theme, or anything more involved like that.

EDIT: To be clear, a class is being added to the parent div of this element, but not the element itself.

1 Answer 1

3

I think it needs to be an array.

Try this:

$form['grant_opportunity_end_date']['#attributes'] = array('class' => array('datepicker'));

You can also call it directly from hook_form_alter(). For example in your themes template.php you could add:

function YOURTHEME_form_alter(&$form, $form_state, $form_id)   {
  if($form_id == 'YOUR_FORM_ID') {
    $form['grant_opportunity_end_date']['#attributes'] = array('class' => array('datepicker'));
  }
  // this displays some general info so you can check
  drupal_set_message("Form ID is : " . $form_id);
  drupal_set_message(dprint_r($form, TRUE));
}
2
  • This seems like it would be correct, but the class isn't appearing on the form.
    – rybosome
    Commented Nov 10, 2011 at 16:31
  • Did you clear the cache in Drupal? CSS and javascript are usually cached so your changes won't show up with a simple refresh.
    – Whiskey
    Commented Nov 10, 2011 at 20:03

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.