1

My scenario looks as follow: I have a form element of checkboxes, e.g. 3. If the rightmost checkbox is marked, the other 2 are greyed out and so on. I don't know how to do this, because if I use the #disabled property all of them are greyed out.

I thought about to make several single checkboxes of type #checkbox and use an ajax call and look at the $form_state and rerender all of them as single ones (disable them then etc.). But there is a problem that I use a form in a drupal block, and I haven't got the &$form_state as a parameter of the function.

What is the best method to do this?

  • 1
    This question appears to be off-topic because it is about code you wrote, for which you want to write better code, or you want to understand why it doesn't work, but you didn't show the code you are using, or the part of the code that is relevant for the question. See Help Center. – Mołot Aug 20 '14 at 12:18
  • I'm note sure to understand (without illustration). You want to disable all the other checkboxes when ONE is checked? So you can't have more than one checkbox checked? Is that correct? – Djouuuuh Aug 20 '14 at 12:21
  • No, if I have e.g. three checkboxes: low, middle, high and I check high, then low and middle is disabled. If middle is checked, only low is disabled etc. – pfu Aug 20 '14 at 12:23
  • 2
    If you want to disable checkbox after selecting one then why not use radios. – DRUPWAY Aug 20 '14 at 12:28
  • Yeah, a mutually exclusive checkbox group is a radio group. No point re-inventing the wheel here, use the elements as they were designed to be used – Clive Aug 20 '14 at 12:37
0

This question is not exactly related to drupal.

Still you can use jQuery to make it work.

Just get the wrapper class of parent element

jQuery('#edit-field-hobby-und .form-checkbox').click(function(){
  jQuery("#edit-field-hobby-und .form-checkbox").each(function(){
  jQuery(this).attr('disabled',true);

  });
  jQuery(this).removeAttr('disabled','disabled')
})

Here form-checkbox is the class of the checkboxes and edit-field-hobby-und is the id of its wrapper.

Hope it will help!

  • Ok with JQuery I'm certain that it works, unfortunately that there is no native drupal way. Thank you – pfu Aug 20 '14 at 12:43
  • 2
    There's no 'native Drupal way' because it wouldn't make sense to have it - the behaviour you're trying to squeeze out of checkboxes is what radios are for – Clive Aug 20 '14 at 12:45
  • 1
    You are probably right, I was to focused on checkboxes~ radios are certainly the answer to solve the problem thanks – pfu Aug 20 '14 at 12:49

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.