Drupal 7 Ajax Forms - select element I've got a custom module form in Drupal 7. The code looks like this:

function form_example_dynamic_form($form, &amp;$form_state) {

$form['tables'] = array(

        '#type' =&gt; 'select',
        '#options' =&gt; drupal_map_assoc(array("2012", "2013")),
        '#title' =&gt; t('Select year:'),

        '#ajax' =&gt; array(
            'callback' =&gt; 'form_example_dynamic_myajax',
            'wrapper' =&gt; 'abcd',
            'effect' =&gt; 'fade',
            'method' =&gt; 'replace',
            ),

       );

 return $form;
 }

 function form_example_dynamic_myajax($form, $form_state) {
     return $form_state['values']['tables'];
 }


So this simple code should update #abcd tag with the value of the 'tables' select. And the problem is that it works only for the first selection. If I chose 2013 it returns "2013" text in my #abcd tag. But, when I chose 2012, an #abcd tag stays unchanged. It still apperas 2013.

Here is the example: http://kuzaj.yamandi.com/pl/form_example_dynamic

Does anyone has any idea how to solve it?
The problem is that Drupal is unable to find the element #abcd in the further calls.

Since you are returning the values alone, it will replace the element #abcd with the value; so the element #abcd will be removed from the document, and it will not be available for further processing. That's why it is not updating the values in the second time.

To overcome this in form_example_dynamic_myajax(), you need to return the values like in the following code.

 function form_example_dynamic_myajax($form, $form_state) {
   return '&lt;div id="abcd"&gt;' . $form_state['values']['tables'] . '&lt;/div&gt;';
 }