Jquery – Drupal CCK add another item callback

ajaxcckdrupaljquery

I'm trying to get the last field item to focus when doing a 'add another item' on a CCK multiple value field.

Here's my code:

 $("#node-form table.content-multiple-table tr.draggable input[type='text']").live("keydown", function (e) {
  if (e.which == 13) {
   $(this).closest("table.content-multiple-table").siblings("div.content-add-more").find("input.form-submit").mousedown();
   $(this).closest("tr.draggable").next().find("input[type='text']").trigger("focus");
   return false;
  }
 });

Clearly this doesn't work because the field is created on AHAH response. 🙁

Anyone know how to hook into the response to focus the last field? Thanks.

Best Answer

You should use Drupal.behaviors and Drupal should manage attaching the behaviors for you. Modules should re run these after modifying the DOM (including AHAH requests).

In your case you will probably want to add two behaviors, one to the button to flag that it was pressed and another on the last field to focus if the flag is set (and unset the flag).