How can I add to the class name of a form element? 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(&amp;$form, &amp;$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.
I think it needs to be an array.

Try this:

$form['grant_opportunity_end_date']['#attributes'] = array('class' =&gt; 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(&amp;$form, $form_state, $form_id)   {
  if($form_id == 'YOUR_FORM_ID') {
    $form['grant_opportunity_end_date']['#attributes'] = array('class' =&gt; 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));
}