How can I avoid extraneous divs in my form? When I create a form using the form API, the resulting html includes all sorts of unwanted divs. For example, if I use:

$form = array(
    'myFieldset' =&gt; array(
        '#type' =&gt; 'fieldset'
    )
);


The resulting html looks like this: 

&lt;form action="/drupal-7.15/?q=chercheur" method="post" id="form-chercheur" accept-charset="UTF-8"&gt;
    &lt;div&gt;
        &lt;fieldset class="form-wrapper" id="edit-myfieldset"&gt;
            &lt;div class="fieldset-wrapper"&gt;
            &lt;/div&gt;
        &lt;/fieldset&gt;
        &lt;input type="hidden" name="form_build_id" value="form-87Dv5ezVpaLI5mD345ofMkLgPhsv_dawudUp73ePQTc" /&gt;
        &lt;input type="hidden" name="form_token" value="sXXINWr7rmParm0tRJoEb6QKGIH_QH-ffRSOtnvxYY0" /&gt;
        &lt;input type="hidden" name="form_id" value="form_Chercheur" /&gt;
    &lt;/div&gt;
&lt;/form&gt;


As you can see, two extraneous divs are added here. They are making the javascript I'm writing uselessly complicated, so I was wondering if there was a way to remove the extraneous divs. Ideally, I would like to obtain something like this:

&lt;form action="/drupal-7.15/?q=chercheur" method="post" id="form-chercheur" accept-charset="UTF-8"&gt;
    &lt;fieldset class="form-wrapper" id="edit-myfieldset"&gt;
    &lt;/fieldset&gt;
    &lt;input type="hidden" name="form_build_id" value="form-87Dv5ezVpaLI5mD345ofMkLgPhsv_dawudUp73ePQTc" /&gt;
    &lt;input type="hidden" name="form_token" value="sXXINWr7rmParm0tRJoEb6QKGIH_QH-ffRSOtnvxYY0" /&gt;
    &lt;input type="hidden" name="form_id" value="form_Chercheur" /&gt;
&lt;/form&gt;


Is this possible?
The first &lt;div&gt; is hard coded into the theme_form() function, so you can override that in your theme's template.php file if you want to. You should be aware, though, that the wrapping &lt;div&gt; is actually there to ensure XHTML compliance; if you remove it your page will fail validation.

That said, this is the code to do it (replacing MYTHEME with the name of your theme):

function MYTHEME_form($variables) {
  $element = $variables['element'];
  if (isset($element['#action'])) {
    $element['#attributes']['action'] = drupal_strip_dangerous_protocols($element['#action']);
  }
  element_set_attributes($element, array('method', 'id'));
  if (empty($element['#attributes']['accept-charset'])) {
    $element['#attributes']['accept-charset'] = "UTF-8";
  }

  return '&lt;form' . drupal_attributes($element['#attributes']) . '&gt;' . $element['#children'] . '&lt;/form&gt;';
}


The only change from the original function is the removal of the &lt;div&gt; and &lt;/div&gt; from the last line, and the removal of the XHTML compliance comment as that no longer applies.

Overriding the fieldset theme is pretty much the same, except you'll be borrowing the code from theme_fieldset():

function MYTHEME_fieldset($variables) {
  $element = $variables['element'];
  element_set_attributes($element, array('id'));
  _form_set_class($element, array('form-wrapper'));

  $output = '&lt;fieldset' . drupal_attributes($element['#attributes']) . '&gt;';
  if (!empty($element['#title'])) {
    // Always wrap fieldset legends in a SPAN for CSS positioning.
    $output .= '&lt;legend&gt;&lt;span class="fieldset-legend"&gt;' . $element['#title'] . '&lt;/span&gt;&lt;/legend&gt;';
  }

  if (!empty($element['#description'])) {
    $output .= '&lt;div class="fieldset-description"&gt;' . $element['#description'] . '&lt;/div&gt;';
  }
  $output .= $element['#children'];
  if (isset($element['#value'])) {
    $output .= $element['#value'];
  }

  $output .= "&lt;/fieldset&gt;\n";
  return $output;
}


Once you clear Drupal's caches the extraneous markup should just disappear.