Add classes to form select option item How can I add classes to a form option tag without JS?  At the moment in Form API I can pass a keyed array like this

array(
  '0' =&gt; 'option 0',
  '1' =&gt; 'option 1',
)


and I'll get html like this

&lt;option value="0"&gt;option 0&lt;/option&gt;
&lt;option value="1"&gt;option 1&lt;/option&gt;


Is there a way to do something like this:

array(
  array(
    'value' =&gt; 0,
    'text' =&gt; 'option 0',
    'class' =&gt; 'bob 0',
  ),
  array(
    'value' =&gt; 1,
    'text' =&gt; 'option 1',
    'class' =&gt; 'bob 1',
  ),
)


and then get this

&lt;option value="0" class="bob 0"&gt;option 0&lt;/option&gt;
&lt;option value="1" class="bob 1"&gt;option 1&lt;/option&gt;
Unfortunately this isn't very easy using the Form API currently.

There's an issue open to add this functionality (it goes all the way back to 2008) that would theoretically allow you to do something like this:

$form['optiontest'] = array(
  '#type' =&gt; 'select',
  '#title' =&gt; t('Option test'),
  '#options' =&gt; array(
    array(
      '#return_value' =&gt; 0,
      '#value' =&gt; t('First option'),
      '#attributes' =&gt; array('class' =&gt; 'first', 'title' =&gt; t('First option')),
    ),
    array(
      '#value' =&gt; t('Option group'),
      '#attributes' =&gt; array('class' =&gt; 'group', 'title' =&gt; t('This is an optgroup')),
      '#options' =&gt; array(
        array('#return_value' =&gt; 2, '#value' =&gt; t('1st sub-option')),
        array('#return_value' =&gt; 4, '#value' =&gt; t('2nd sub-option')),
      ),
    ),
  ),
);


But unfortunately there are nothing but failed patches attached to the issue at the moment.

The only way I can think of to do it at the moment would be to add a #process function to the select element, and add the class(es) to each option when they're broken down individually.