I'm making a module and on the administration page there is a form and I want to make one of the fields appears only when user check on a checkbox. Like the option to add a new content to a menu.

  • Please add the code snippet of the custom module for better understanding. – DRUPWAY Sep 20 '14 at 12:33
  • It's a normal form ,,, $form[] – Ahmed AlQahtani Sep 20 '14 at 14:16
  • the simplest method, without having to get into ajax callbacks, etc - is to add the elements to the form from the start, and then a bit of jquery to hide, and then show the element you want – Geoff Sep 20 '14 at 15:02

You can use #states from FormAPI that drupal provides. Below is the link https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#states

Below is the code from drupal form api

$form['email_canceled']['settings'] = array(
    '#type' => 'container',
    '#states' => array(
      // Hide the settings when the cancel notify checkbox is disabled.
      'invisible' => array(
       ':input[name="email_canceled_toggle"]' => array('checked' => FALSE),

Also you can use like this

$form['field_image']['#states']['visible'] = array(':input[name="field_checkbox[und]"]' => array('value' => 1));

so only when the check box is checked the field image will be displayed otherwise it will be hidden.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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