Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I need to toggle a textbox depending upon the checkbox value.

Basically i need to take email entries for updates.

Here is the form elements I used.

$form['chkupdates'] = array(
        '#type' => 'checkbox',
        '#title' => t('Sign up to receive email updates about Green Apple'),
        '#options' => array(
        '1' => t('Yes'),
        '0' => t('No')
        ),
        '#default_value' => FALSE,
        '#attributes' => Array('class'=>'sub-form-element',
        'id'=>'chkbox'),
);

$form['chkmail'] = array(
'#type' => 'textfield',
'#title' => t(''),
'#attributes' => Array('class'=>'field','id'=>'chktext'),
);

Please guide.I am rendering elements in tpl files so jQuery is acceptable. Thanks

share|improve this question

1 Answer

up vote 1 down vote accepted

You could do something along these lines:

$('#CHKBOX').click( function() {

  if ($('#CHKBOX').is(':checked')) {
    $('#CHKTEXT').display();
  } else {
    $('#CHKTEXT').hide();
  }

});

where CHKBOX and CHKTEXT are the CSS ids of the form elements in the resulting HTML. If you know it will be hidden at the start, this can be simplified further into:

$('#CHKBOX').click( function() {
  $('#CHKTEXT').toggle();
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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