Dynamically hiding / showing Field API fields in Drupal 7 I've created an entity with an 'add new' form. The entity itself has a limited number of actual variables. I've added most of the extra data I need using custom Fields (i.e. the Field API).

What I need to do at this stage is be able to dynamically hide one field based on the value of another. i.e. if a drop-down field has it's value set to No, another field should be hidden, otherwise it should be shown.

From what I can see, it is somewhat easy to add this functionality to fields created using the Form API (i.e. via the AJAX property), however is there any way to achieve it using attached fields? I have no issues using custom Javascript if that is what is required to solve this.
In Drupal 7 you can use $form #states instead of custom jQuery script. Example:

  $form['student_type'] = array(
    '#type' =&gt; 'radios',
    '#options' =&gt; array(
      'high_school'   =&gt; t('High School'),
      'undergraduate' =&gt; t('Undergraduate'),
      'graduate'      =&gt; t('Graduate'),
    ),
    '#title' =&gt; t('What type of student are you?')
  );

  // High school information.
  $form['high_school']['tests_taken'] = array(
    '#type' =&gt; 'checkboxes',
    '#options' =&gt; drupal_map_assoc(array(t('SAT'), t('ACT'))),
    '#title' =&gt; t('What standardized tests did you take?'),
    // This #states rule says that this checkboxes array will be visible only
    // when $form['student_type'] is set to t('High School').
    // It uses the jQuery selector :input[name=student_type] to choose the
    // element which triggers the behavior, and then defines the "High School"
    // value as the one that triggers visibility.
    '#states' =&gt; array(
      'visible' =&gt; array(   // action to take.
        ':input[name="student_type"]' =&gt; array('value' =&gt; 'high_school'),
      ),
    ),
  );


Here is example if you want to use #states for multiple values condition:

 $form['student_type'] = array(
    '#type' =&gt; 'checkboxes',
    '#options' =&gt; array(
      'high_school'   =&gt; t('High School'),
      'undergraduate' =&gt; t('Undergraduate'),
      'graduate'      =&gt; t('Graduate'),
    ),
    '#title' =&gt; t('What type of student are you?')
  );

  // High school information.
  $form['high_school']['tests_taken'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('What standardized tests did you take?'),
    '#states' =&gt; array(
      'visible' =&gt; array(   // action to take.
        ':input[name="student_type[high_school]"]' =&gt; array('checked' =&gt; TRUE),
        ':input[name="student_type[undergraduate]"]' =&gt; array('checked' =&gt; TRUE),
        ':input[name="student_type[graduate]"]' =&gt; array('checked' =&gt; FALSE),
      ),
    ),
  );


See the form_example/form_example_states.inc from examples module for more details and examples.