Can I use ajax to target multiple form elements from one input?
I'm trying to use ajax to update two different parts of a drupal form from just one form input blur.
I have the standard ajax stuff on my input:
$my_form['my_input']['#ajax'] = array( 'callback' => 'my_callback', 'wrapper' => 'my_target_div', 'event' => 'blur', );
This is working well, swapping out my div when the input is updated ... however I also want to update another form input elsewhere in the form with different code as a result of the blur on my original input.
Is this possible, any ideas?
Edit: For clarity, here's my real world example:
- Content type 'movie'
- Added field 'primary_title'
- When 'primary_title' is updated my ajax callback checks for similar strings and returns html.
The html from the callback is inserted into an empty div.
That part is working great!
I'm trying to also modify the standard node 'title' input, giving it the value of 'primary_title' after I have done some regex to tidy up the string (removing "The" or "A" from the start etc) The result will be two title fields, one with the complete title 'primary_title' and one trimmed 'title' that will be useful for sorting and displaying records.
It appears that you can target different parts of a form using ajax commands, however you must choose to return only one of either:
A renderable array or
An array of AJAX commands
In the situation I originally wrote about, I was trying to return both an array of AJAX commands and a renderable array, which doesn't seem possible.
Code in the examples module demonstrates targeting different parts of a form using an array of AJAX commands:
In this code sample, #html_div is getting $text, and separately #html_status is getting "Updated html_command_example with text=$text; " . date('r') . ""
Two different locations on a form with two different pieces of information from the one ajax callback!
Looking again at what I was working on, I realized that I didn't need to return the node title field at all. It will only be a utility field, used for sorting data in lists.
I have hidden the field on the node form and it is populated the second time my form is built when my original ajax callback is triggered.
I am quite new to Drupal but so far i found the Ajax framework rather unflexible. It works great as long as you use it as it was intended to. So in your instance if you know some JS the best would definilty be to make that script your self it should be very light.
Another option, from drupal.org:
Your callback funciton could be as simple as:
That should be called from an
#ajaxarray on a form element (
field_whateverfor the example below) defined in a
hook_form_FORM_ID_alter()(or some kind of form alter hook) where you also test for
$form_stateand make alterations to the