Form loaded with ajax call not having autocomplete enabled I have a form (loaded by obiumap function) that have a section loaded ( by function obiumap_addobj() ) with a $.ajax call from jQuery script (addobj.js) by clicking a button (addobj_btn). This new section is loaded in an 

existing div (addobj-container) but without autocomplete functionality and also without default_value.

This is main form:

function obiumap($form, &amp;$form_state) {
drupal_add_js(drupal_get_path('module', $this-&gt;modName) . '/addobj.js');
global $user;

$form=array();
$form['uid'] = array(
'#type' =&gt; 'hidden',
'#title' =&gt; 'uid',
'#default_value' =&gt; '3',
'#id' =&gt; 'uid',
);  
$form=array();
$form['name'] = array(
'#type' =&gt; 'textfield',
'#title' =&gt; 'name',
'#id' =&gt; 'name',
);  
$form['addobj_btn'] = array(
     '#type' =&gt; 'button',
     '#value' =&gt; t('Add new objects'),
     '#id' =&gt; 'addobj_btn'
);  
$form['addobj-container'] = array(
        '#markup' =&gt;  '&lt;div id="addobj-container" &gt;&lt;/div&gt;'
);
die(drupal_render_children($form));
}


This is section definition loaded via ajax:

function obiumap_addobj($form, &amp;$form_state){
global $user;
$form=array();
$form['uid'] = array(
'#type' =&gt; 'textfield',
'#title' =&gt; 'uid',
'#default_value' =&gt; '10',
'#id' =&gt; 'uid',
);  
$form['fldset_addobj'] = array(
'#type' =&gt; 'fieldset',
'#title' =&gt; t('Add new object'),
'#collapsible' =&gt; TRUE,
'#collapsed' =&gt; FALSE,
);
$form['fldset_addobj']['objname'] = array(
'#type' =&gt; 'textfield',
'#id' =&gt; 'objname',
'#title' =&gt; t('Object name'),
'#autocomplete_path' =&gt; 'obimap/obiumap_addobj',
'#required' =&gt; TRUE,
);
 $form['fldset_addobj']['submit'] = array(
'#type' =&gt; 'submit',
'#value' =&gt; 'Add object',
'#id' =&gt; 'addobj_btn'
);
die(drupal_render_children($form));
}   


and this is addobj.js

function addItems(arg1, arg2) {
jQuery(document).ready(function() {
jQuery('#addobj_btn').click(function() {  
var formURL = 'obiumap_addobj';
jQuery("#obiumap").submit(function(){
 return false;
 });
    jQuery.ajax(
    {
        url : formURL,
        type: "GET",
        data : { uid: jQuery('input[name=uid]').val() },
        success:function(data, textStatus, jqXHR)
        {
            jQuery('#addobj-container').html(data);
            console.log("###### Drupal.attach #####");
            Drupal.attachBehaviors("#addobj-container");
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
        }
    });

});

});
}


I really need autocomplete for that field but I don't now how to activate it.
If you want Drupal's autocomplete, you need to modify form using Form API '#ajax'. If you are doing it with your custom scripts, Form API will not know that field really belongs to your form, and will not handle field's calls.1

The way you should do it is:


In your form building function, create element with '#ajax' property, that will be responsible for loading your form part, or add it to existing element.
Also in your form building function, add an if that will check triggering element ($form_state['triggering_element']), and if that was your button, add entry in $form_state that new section should be visible.
If there is an entry mentioned in 2., output form part you want to be loaded with AJAX.
Use AJAX callback function only to select and return proper part of the form.




1 Simplified explanation, of course.