AJAX in Form API element returned in another AJAX callback I have a form like this:

$form['my_button'] = array(
  '#type' =&gt; 'button',
  '#ajax' =&gt; array(
    'callback' =&gt; 'my_callback_1',
    'wrapper' =&gt; 'some_wrapper',
  )
);


Which works fine (code is very simplified). Now, in the callback, I want to return another button, which also has ajax configured:

function my_callback_1() {
  return array(
    'my_other_button' =&gt; array(
      '#type' =&gt; 'button',
      '#ajax' =&gt; array(
        'callback' =&gt; 'my_callback_2',
        'wrapper' =&gt; 'some_other_wrapper',
      )
    ),
  );
}

function my_callback_2() {
  return array('#markup' =&gt; 'success!!');
}


Now, when I click on the first button, Drupal loads the second button just fine. The problem is, a click on the second button does nothing. Am I missing something?
After a lot of reading, I solved this by returning $form on the AJAX callback, and including the second button on a form rebuild. My code now looks more like this, and it's working fine:

function my_form($form, &amp;$form_state) {
  $wrapper_id = drupal_html_id('some_wrapper');

  $form['#prefix'] = '&lt;div id="' . $wrapper_id . '"&gt;';
  $form['#suffix'] = '&lt;/div&gt;';

  if (isset($form_state['my_flag']) &amp;&amp; $form_state['my_flag']) {
    $form['my_other_button'] = array(
      '#type' =&gt; 'button',
      '#name' =&gt; 'my_other_button',
      '#ajax' =&gt; array(
        'callback' =&gt; 'my_callback',
        'wrapper' =&gt; $wrapper_id,
      )
    );
  }
  else {
    $form['my_button'] = array(
      '#type' =&gt; 'submit',
      '#name' =&gt; 'my_button',
      '#ajax' =&gt; array(
        'callback' =&gt; 'my_callback',
        'wrapper' =&gt; $wrapper_id,
      ),
    );
  }
}

function my_form_submit($form, &amp;$form_state) {
  switch ($form_state['triggering_element']['#name']) {
    case 'my_button':
      //code for when user presses the first button
      $form_state['my_flag'] = TRUE; //show the other button
      $form_state['rebuild'] = TRUE; //this is necessary!
      break;
    case 'my_other_button':
      //code for when user presses the second button
      break;
  }
}

function my_callback($form, $form_state) {
  return $form;
}


I think the problem had to do with a button being outside an actual form. I hope this helps others...