Re-Attach Drupal AJAX behaviors to a form element

Recently I encountered a problem with Drupal’s AJAX controls where a file upload field would try to upload something and fail because the file was too large. Drupal does a good job of showing an error, but now the form was missing. When I figured out adding the form again… the JS events were not working.

Turns out that Drupal uses jQuery’s bind() method. So when the form is destroyed during the upload the event handler is lost. So we need a means to re-add the behaviors. Turns out we can do:

So far this is the only solution for this issue I’ve found, but I’m not totally sure it is the best. What do you think?