Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Disable radio buttons after selection

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 10 months ago - 7 years 10 months ago #136891 by tpartner
Replied by tpartner on topic Disable radio buttons after selection
Okay, that was a typo on my part - forgot the $(document).ready() function.

Here is an extended version that:
1) Fixes the core event handler on the label div elements (that should never act on disabled radios)
2) Adds a class to all disabled answer rows so they can be styled as in the CSS example below
3) Handles the "Other" text input
3) Handles initial states in case of returning to the group

Script:
Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function(){ 
 
    // A function to disable all non-checked radios
    function handleDisableNonChecked(thisRadio) {
      var thisRow = $(thisRadio).closest('.answer-item');
      // Disable non-checked radios
      $('#question{QID} input.radio').not(thisRadio).prop('disabled', true);
      // Handle "Other" text input
      if($('input.text', thisRow).length == 0) {
        $('#question{QID} input.text').prop('disabled', true);
      }
      // Add a class to the disabled rows
      $('#question{QID} .answer-item').not(thisRow).addClass('disabled-row');
    }
 
    // Initial state (in case of returning to page
    if($('#question{QID} input.radio:checked').length > 0) {
      handleDisableNonChecked($('#question{QID} input.radio:checked'));
    }
 
    // Listener on the radios
    $('#question{QID} input.radio').on('click', function(e){
      handleDisableNonChecked(this);
    });
 
    // Fix the core event handler for the label div 
    $('#question{QID} .label-clickable').unbind('click').on('click', function(e){
      if(!$(this).parent().find('input.radio').is(':disabled')) {
        $(this).parent().find('input.radio').trigger('click');
      }
    });
  });
</script>

CSS:
Code:
.disabled-row .label-clickable{
    opacity: 0.45;
  cursor: not-allowed;
}

Modified test survey:

File Attachment:

File Name: limesurvey...(v2).lss
File Size:16 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 7 years 10 months ago by tpartner.
The topic has been locked.
  • Deusdeorum
  • Deusdeorum's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 10 months ago - 7 years 10 months ago #136897 by Deusdeorum
Replied by Deusdeorum on topic Disable radio buttons after selection
Really nice answer Tony, your first solution worked up until I read

3) Handles initial states in case of returning to the group

So your latter solution is preferred and works great!
Last edit: 7 years 10 months ago by Deusdeorum.
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 10 months ago #136913 by holch
Replied by holch on topic Disable radio buttons after selection
I tried it without adding the CSS to the template and it seems to work fine. I assume the css only makes the lables less visible, right?

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 10 months ago #136919 by tpartner
Replied by tpartner on topic Disable radio buttons after selection
Correct, and in supporting browsers the cursor will be a red circle with a diagonal line.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose