Search code examples
drupaldrupal-6drupal-modules

How do I add an onchange handler to a dropdown list in Drupal?


How can I add an onchange handler to a dropdown list in Drupal? The dropdown list is added using hook_form(). I have to perform a function depending on the onchange function. Is there a way to do this?


Solution

  • You can add a form like this:

     hook_form()
     {
       $form1["dropdown"] = array(
        '#type' => 'select',
        '#title' => t('Preview the page with themes available'),
        '#options' => $ptions,
        '#default_value' => 'defalut_value',
        '#attributes' => array('onchange' => "form.submit('dropdown')"),
     );
    //Submit button:
    $form1['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit '),
            '#attributes' => array('style' => 'display: none;'),
    );
    

    Now you can add submit functionality using hook_submit().