title: Adding attribute to option element using forms api : drupal 7
body: <p>I want to add <strong>title="icons/icon_cart.gif"</strong> for each of the below options in my select list which is rendered using views.</p>
  
  <p>After trying and reading many articles I can't seem to find the way to add this html into my form.</p>
  
  <p>Below is my code. </p>
  
  <pre><code>function customchatter_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  
  $form["tid"]["#options"][1]=t("nooo chatter");
  // this works to change the label of the option but how do I add title="icons/icon-  
  cart.gif" ?
  
  }
  </code></pre>
  
  <p>My html code:</p>
  
  <pre><code>&lt;select id="edit-tid" name="tid" class="form-select"&gt;
  &lt;option value="All"&gt;- Any -&lt;/option&gt;
  &lt;option value="1"&gt;nooo chatter&lt;/option&gt;
  &lt;option value="2"&gt;Complaints Complaints&lt;/option&gt;
  &lt;option value="3"&gt;Gear &amp;amp; Gadgets&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>Cheers,
  Vishal</p>
  
  <p><strong>UPDATE</strong>
  I tried to update the code as per Clive's advice but the values are still not coming up right. Below is my story.</p>
  
  <p>So below is the html output I am able to achieve but the title always seems to be the number 1. </p>
  
  <pre><code>&lt;select id="edit-select" class="form-select" name="select"&gt;
  &lt;option value="1" title="1"&gt;One&lt;/option&gt;
  &lt;option value="2" title="1"&gt;Two&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p><em>As you can see title is there but the value is wrong. Below is my form and the functions which I wrote.</em></p>
  
  <p><strong>My form:</strong></p>
  
  <pre><code>$form['select'] = array(
  '#type' =&gt; 'select',
  '#options' =&gt; array(1 =&gt; 'One', 2 =&gt; 'Two'),
  '#title' =&gt; array(1 =&gt; 'One', 2 =&gt; 'Two') 
  // I tried many combinations but nothing seems to work.
  );
  </code></pre>
  
  <p><strong>My Theme functions.</strong></p>
  
  <pre><code>function kt_vusers_select($variables) {
  $element = $variables['element'];
  element_set_attributes($element, array('id', 'name', 'size'));
  _form_set_class($element, array('form-select'));
  
  return '&lt;select' . drupal_attributes($element['#attributes']) . '&gt;' .    
  kt_vusers_form_select_options($element) . '&lt;/select&gt;';
  }
  
  
  function kt_vusers_form_select_options($element, $choices = NULL) {
  
  if (!isset($choices)) {
  $choices = $element['#options'];
  }
  // array_key_exists() accommodates the rare event where $element['#value'] is NULL.
  // isset() fails in this situation.
  $value_valid = isset($element['#value']) || array_key_exists('#value', $element);
  
  
  // @vishal  so there I have declared the variable to accept the values.
  $vtitle = isset($element['#title']) || array_key_exists('#title', $element);
  
  
  $value_is_array = $value_valid &amp;&amp; is_array($element['#value']);
  $options = '';
  foreach ($choices as $key =&gt; $choice) {
  if (is_array($choice)) {
    $options .= '&lt;optgroup label="' . $key . '"&gt;';
    $options .= form_select_options($element, $choice);
    $options .= '&lt;/optgroup&gt;';
  }
  elseif (is_object($choice)) {
    $options .= form_select_options($element, $choice-&gt;option);
  }
  else {
    $key = (string) $key;
    if ($value_valid &amp;&amp; (!$value_is_array &amp;&amp; (string) $element['#value'] === $key || 
  ($value_is_array &amp;&amp; in_array($key, $element['#value'])))) {
      $selected = ' selected="selected"';
    }
    else {
      $selected = '';
    }
  
   // @vishal this is where the variable is being used.
  
   $options .= '&lt;option title="'.$vtitle.'" value="' . check_plain($key) . '"' . $selected . 
   '&gt;' . check_plain($choice) . '&lt;/option&gt;';
  }
  }
  return $options;
  }
  </code></pre>
  
  <p>below is the correct code for the last theme function</p>
  
  <pre><code>function kt_vusers_form_select_options($element, $choices = NULL, $vtitles=NULL) {
  // Build up your own version of form_select_options here
  // that takes into account your extra attribute needs.
  // This will probably involve inspecting your custom FAPI property,
  // which we'll call #extra_option_attributes
  
  if (!isset($choices)) {
  $choices = $element['#options'];
  $vtitles = array();
  $vtitles = $element['#title'];
  }
  // array_key_exists() accommodates the rare event where $element['#value'] is NULL.
  // isset() fails in this situation.
  $value_valid = isset($element['#value']) || array_key_exists('#value', $element);
  
  $value_is_array = $value_valid &amp;&amp; is_array($element['#value']);
  $options = '';
  
  //  print_r($vtitles);
  </code></pre>
  
  <p>while
     (
      (list($key, $choice) = each($choices))
      &amp;&amp; (list($keytwo, $vtitle) = each($vtitles))
     )
     {
      // printf("%s => %s, %s => %s \n", $key1, $value1, $key2, $value2);</p>
  
  <pre><code> if (is_array($choice)) {
      $options .= '&lt;optgroup label="' . $key . '"&gt;';
      $options .= kt_vusers_form_select_options($element, $choice);
      $i++;
      // $options .= form_select_options($element, $vtitle);
      $options .= '&lt;/optgroup&gt;';
      } // end if if is_array
  
   elseif(is_object($choice)) {
    $options .= form_select_options($element, $choice-&gt;option);
    } // end of else if
  
  
  
  else {
        $key = (string) $key;
    if ($value_valid &amp;&amp; (!$value_is_array &amp;&amp; (string) $element['#value'] === $key ||  
  ($value_is_array       &amp;&amp; in_array($key, $element['#value'])))) {
      $selected = ' selected="selected"';
      }
    else {
      $selected = '';
    }
    // $options .= '&lt;option title="'.$vtitle.'" value="' . check_plain($key) . '"' . 
    $selected . '&gt;' . check_plain($choice) . '&lt;/option&gt;';
  
  
   }
  
   $options .= '&lt;option value="'. check_plain($key) .'" title="' . $vtitle . '"' . $selected 
   .'&gt;'. check_plain($choice) .'&lt;/option&gt;';
  
  
  
   } // end of choice
  
  
  
  return $options;
  
  
  } // end of function
  </code></pre>