drupal: Form API, dynamically hide or show fields based on input

You can use the #states property to achieve that. The #states property can be applied to all Form API elements.

Here's the documentation link with an example.


simple usage example of #states: To show a select field with name 'item' only if another field with name 'type' has value 'sell'

$form['item'] = array( 
        '#title' => t('Task Item'),
        '#type' => 'select',
        '#states' => array(
            // Only show this field when the value of type is sell.
            'visible' => array(
                ':input[name="type"]' => array('value' => 'sell'),
            ),
        ),
    );