form ajax forget's changes made in previous ajax callback calls Why do changes don't get saved in $form, or what is it that's overriding it?

I would like to set custom class to multiple fieldsets in an ajax callback function:

if (something) {
  $form['fieldset_01']['#attributes'] = array('class' =&gt; array('custom_class'));
  $form['fieldset_02']['#attributes'] = array('class' =&gt; array('custom_class'));
}
elseif (somethingelse) {
  $form['fieldset_11']['#attributes'] = array('class' =&gt; array('custom_class'));
  $form['fieldset_12']['#attributes'] = array('class' =&gt; array('custom_class'));
} 

return $form;


When $form is returned, the element that I have set in this call is rendered fine, with a new CSS class but the elements I have rendered in previous calls have lost this new class, as if they were rendered with default values.

EDIT: my form creation function 

function mynode_form($form, &amp;$form_state) {

$nid = $form_state['values']['nid'];

$ajax = array(
'callback' =&gt; 'mynode_ajax',
'wrapper' =&gt; 'mynode_form',        
'event' =&gt; 'change',
'method' =&gt; 'replace',
);

$node = node_load($nid);
$title = $node-&gt;field_title['und'][0]['value'];              

$form = array();
$form['#prefix'] = '&lt;div id="mynode_form"&gt;';
$form['#suffix'] = '&lt;/div&gt;';

$form['nid'] = array(
  '#value' =&gt; $nid,
  '#type' =&gt; 'hidden',
);

$form['fieldset_0'] = array(
  '#type' =&gt; 'fieldset',
  '#collapsible' =&gt; true,    
  '#title' =&gt; 'Fieldset title 0',      
  //'#attributes' =&gt; array('class' =&gt; array('a', 'b')),
  '#weight' =&gt; 1,
);

$form['fieldset_0']['textfield_0'] = array(
  '#type' =&gt; 'textfield',
  '#title' =&gt; t('Title 0?'),        
  '#ajax' =&gt; $ajax,    
);

$form['fieldset_1'] = array(
  '#type' =&gt; 'fieldset',
  '#collapsible' =&gt; true,    
  '#title' =&gt; 'Fieldset title 1',      
  //'#attributes' =&gt; array('class' =&gt; array('a', 'b')),
  '#weight' =&gt; 1,
);

$form['fieldset_1']['textfield_1'] = array(
  '#type' =&gt; 'textfield',
  '#title' =&gt; t('Title 1?'),        
  '#ajax' =&gt; $ajax,    
);

if (isset($form_state['triggering_element']['#name'])){
  $key = $form_state['triggering_element']['#name'];
  switch($key){
    case 'textfield_0':            
      $form['fieldset_0']['#attributes'] = array('class' =&gt; array('a', 'b',   'c'));                                    
      $form['fieldset_1']['#attributes'] = array('class' =&gt; array('a',   'b'));
    break;    
  }
}
else{
  //only the first time
  $form['fieldset_0']['#attributes'] = array('class' =&gt; array('a', 'b'));
  $form['fieldset_1']['#attributes'] = array('class' =&gt; array('a', 'b'));
}     
return $form;
}
AJAX callback function is meant to only tell Drupal what to update. By design all you do in it is for one call only.

Permanent changes needs to be done in main form creation function. I usually see them done in block like that:

if (!empty($form_state['values'])) {
  //here you are supposed to use these values
}