Drupal 7 introduced the #states
property for form elements. It:
Adds JavaScript to the element to allow it to have different active states.
Here's a short example that applies to your use case:
function MYMODULE_my_form($form, &$form_state) {
$form['my_radios'] = array(
'#type' => 'radios',
'#title' => 'Radios',
'#options' => array('no' => 'No', 'yes' => 'Yes'),
'#default_value' => 'no'
);
$form['textarea'] = array(
'#type' => 'textarea',
'#title' => 'Textarea',
'#states' => array(
'visible' => array(
':input[name="my_radios"]' => array('value' => 'yes')
)
)
);
return $form;
}
The above assumes you're building the form yourself; if you're not, you can combine this logic with a hook_form_alter()
function. This example assumes you're adding it to a node add/edit form for the Basic Page content type:
function MYMODULE_form_page_node_form_alter(&$form, &$form_state, $form_id) {
$form['textarea']['#states'] = array(
'visible' => array(
':input[name="name_of_radios_element"]' => array('value' => 'yes')
)
);
}
If you're using the second method you'll need to do some digging into the $form
array to find out exactly which element you need to target. If you don't already have it I recommend installing the Devel module and using it's excellent dpm()
function for your debugging.
There's more information on the various different options for #states
on the drupal_process_states()
docs page.