views:

409

answers:

3

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.

A: 

Use last: http://api.jquery.com/last

Nikit
Thanks but the question is about hooking on the response, not getting the last.
Jourkey
+1  A: 

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).

Jeremy French
Thanks, I'm trying it.
Jourkey
+1  A: 

Looking at the source code of cck/includes/content.node_form.inc, and more precisely this part in the content_add_more_js function:

  // Build our new form element for the whole field, asking for one more element.
  $form_state['item_count'] = array($field_name => count($_POST[$field_name]) + 1);
  $form_element = content_field_form($form, $form_state, $field);
  // Let other modules alter it.
  drupal_alter('form', $form_element, array(), 'content_add_more_js');

I'd say you'll need to setup a hook_form_alter where the form_id is 'content_add_more_js', as it seems this is called after generating the new field, but before adding it to the page.

I haven't tried that yet though...


Oussama Mubarak // Semiaddict

Semiaddict