How do I re-attach AJAX behaviours to a managed_file field? I currently have a form which is generated by the FAPI via an jQuery AJAX call, and then is inserted into the DOM. 

This works fine, but the problem is that managed_file field within the newly generated form have lost all their AJAX behaviours. I've tried a couple of things to try get Drupal to reattach the behaviours after the form is generated but I'm having no luck. 

Here's my code for retrieving and inserting the new form into the DOM:

$.ajax({
type: 'POST',
url: '/example-get-form',
dataType: 'json',
data: 'get=1',    
success: function(response){

  //insert new form into DOM
  var newForm = response.data.form;
  $('.profile-form.edit').append(newForm);
  Drupal.attachBehaviors();
}
});


As you can see from the frontend code, we retrieve the rendered form HTML via AJAX and then insert it into our container div with the selector ".profile-form.edit". The form is rendered and inserted into the DOM correctly, but the problem is that the dyanmic form's managed_file field can either be empty or contain an image, so both the "Upload" and "Remove" buttons needs to have their AJAX behaviours reattached.

Here's the code for generating the form via the callback URL called from my jQuery script:

//generate form and send via JSON
$response['data']['form'] = drupal_get_form('mymodule_example_form');  
$response['data']['form'] = render($response['data']['form']);

return drupal_json_output($response);
drupal_exit();


Any help to put me on the right track would be greatly appreciated!
The main problem is that you're not loading the form in Drupal way in the AJAX requests. When you use the normal jQuery ajax to load the form, then the necessary actions(Generating new scripts and re-attaching the behaviors) won't happen.

So you should follow the below process to achieve it.


Create a page with ajax Link/button - clicking on it should load the form(This
may change based on your need)
Create form generation callback
Create a ajax callback which will returns the form


Below is the complete example module which loads the form with the managed file field using ajax.

&lt;?php

/**
 * @file
 * Provides File module pages for testing purposes.
 */

/**
 * Implements hook_menu().
 */
function test_menu() {
  $items = array();

  $items['file/test'] = array(
    'title' =&gt; 'Managed file test',
    'page callback' =&gt; 'drupal_get_form',
    'page arguments' =&gt; array('test_form'),
    'access arguments' =&gt; array('access content'),
  );
  $items['ajax/test'] = array(
    'title' =&gt; 'Managed file test',
    'page callback' =&gt; 'file_ajax_page',
    'access arguments' =&gt; array('access content'),
  );
  $items['ajax/form'] = array(
    'title' =&gt; 'Managed file test',
    'page callback' =&gt; 'file_ajax_form',
    'access arguments' =&gt; array('access content'),
  );

  return $items;
}

/**
 * Generates the Test page.
 */
function file_ajax_page() {
    // Make sure Drupal Ajax framework javascript is around.
  drupal_add_library('system', 'drupal.ajax');
  $output = '';
  // Generate a Ajax Link to load the form.
  $output .= l(t('Delete'), 'ajax/form', array('attributes' =&gt; array('class' =&gt; 'use-ajax')));
  // Specify the container for the form.
  $output .= '&lt;div class="form-container"&gt;&lt;/div&gt;';
    return $output;
}

/**
 * Returns the processed file form.
 */
function file_ajax_form() {
    $commands = array(); 

    // Treat them in Drupal way to load the form in AJAX, so that all the necessary scripts will be loaded.
  $commands[] = ajax_command_html('.form-container', drupal_render(drupal_get_form('test_form'))); 
  ajax_deliver( array(
    '#type' =&gt; 'ajax',
    '#commands' =&gt; $commands,
  ));
}

/**
 * Form constructor for testing a 'managed_file' element.
 *
 * @see file_module_test_form_submit()
 * @ingroup forms
 */
function test_form($form, &amp;$form_state, $tree = TRUE, $extended = FALSE, $default_fid = NULL) {
  $form['#tree'] = (bool) $tree;

  $form['nested']['file'] = array(
    '#type' =&gt; 'managed_file',
    '#title' =&gt; t('Managed file'),
    '#upload_location' =&gt; 'public://test',
    '#progress_message' =&gt; t('Please wait...'),
    '#extended' =&gt; (bool) $extended,
    '#size' =&gt; 13,
  );
  if ($default_fid) {
    $form['nested']['file']['#default_value'] = $extended ? array('fid' =&gt; $default_fid) : $default_fid;
  }

  $form['textfield'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Type a value and ensure it stays'),
  );

  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Save'),
  );

  return $form;
}

/**
 * Form submission handler for file_module_test_form().
 */
function test_form_submit($form, &amp;$form_state) {
  if ($form['#tree']) {
    $fid = $form['nested']['file']['#extended'] ? $form_state['values']['nested']['file']['fid'] : $form_state['values']['nested']['file'];
  }
  else {
    $fid = $form['nested']['file']['#extended'] ? $form_state['values']['file']['fid'] : $form_state['values']['file'];
  }
  drupal_set_message(t('The file id is %fid.', array('%fid' =&gt; $fid)));
}


This way Drupal will do all the works for you if you specify what/how do you want:)

I took File Test module in the core as the base for this example.