Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As given in the code below, I tried enabling select list $form['answer']['answer1'] through AJAX callback, textbox is getting populated with value, where as the dropdownlist is not getting enabled. Any help?

function mymodule_search_block_form($form, &$form_state) {
  $options = array();
  $options['key1'] = t('value1');
  $options['key2'] = t('value2');
  $options['key3'] = t('value3');
  $options['key4'] = t('value4');

  $form['profiles'] = array(
    '#type' => 'select',
    '#title' => 'Profiles',
    '#options' => $options,
    '#ajax' => array(
        'callback' => 'ajax_select_callback',
        'wrapper' => 'profile-wrapper',
        'event' => 'change',

  $form['answer']['profile_name'] = array(
    '#prefix' => '<div id="profile-wrapper">',
    '#type' => 'textfield',
    '#title' => 'Profile Name1',
  $form['answer']['profile_name2'] = array(
    '#type' => 'textfield',
    '#title' => 'Profile Name2',
  $form['answer']['answer1'] = array(
    '#type' => 'select',
    '#disabled' => 1,
    '#title' => 'Profiles',
    '#options' => $options,
    '#suffix' => '</div></br>',
  return $form;

function ajax_select_callback($form, $form_state) {
  $form['answer']['profile_name']['#value'] = 'Profile 1';
  $form['answer']['profile_name2']['#value'] = 'Profile 2';
  $form['answer']['answer1']['#disabled'] = 0;
  return $form['answer'];
share|improve this question
up vote 3 down vote accepted

Interesting question. The way I see it there are actually two parts to this. This first is, of course, your actual question and that can be easily solved:

// Your code in ajax_select_callback().
$form['answer']['answer1']['#disabled'] = 0;
// More of your code in ajax_select_callback().

When you set the #disabled property of a form element to TRUE, what Drupal does is simply add the disabled attribute to the element and allows the renderer (i.e. drupal_render()) to do its job. In your AJAX callback, the $form array passed is that of the rendered form. This means that the your dropdown element already has the disabled attribute on it. Therefore, when it is rendered again, it still shows up disabled. So, to prevent this, we simply remove the attribute.

However, you'd notice that when you submit your form after this, the value of the disabled dropdown (in $form_state['values']) may not be the value you chose after "enabling" it. This is because your form is not being re-built on the server which means that Drupal still thinks your dropdown is disabled and will refuse to process your input, simply ignoring it and using the default. If you are able to reproduce this, comment on this answer and I'll update this post with a solution for that problem.

share|improve this answer
Thanks it worked. – Kiran Kumar Dec 26 '12 at 6:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.