3

I have a problem to make a date and a list element in a form in HTML invisible if the user selects one way, and visible if the user selects two. How can I write the JavaScript code for that?

1 Answer 1

2

No need to write JavaScript for that; if you are working in PHP, you can use #states on the form element.

Example: form_example_states_form.

  $form['high_school'] = array(
    '#type' => 'fieldset',
    '#title' => t('High School Information'),

    // This #states rule says that the "high school" fieldset should only
    // be shown if the "student_type" form element is set to "High School".
    '#states' => array(
      'visible' => array(
        ':input[name="student_type"]' => array('value' => 'high_school'),
      ),
    ),
  );
5
  • but how if i'm using radio button to select.Does the same code can be used in php?? Commented May 7, 2013 at 2:40
  • Yes, as written in the doc link above, #states can be used with any form element Commented May 7, 2013 at 3:07
  • with the secret santa knowledge that #states is really emitting some simple jquery to show/hide stuff site since the action => condition array is just basically jquery selectors. A useful brief tutorial is at randyfay.com/states
    – tenken
    Commented May 7, 2013 at 3:48
  • tq very much @DavidThomas Commented May 8, 2013 at 8:55
  • tq also @tenken Commented May 8, 2013 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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