Welcome to the LimeSurvey Community Forum

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

Disable radio buttons after selection

More
9 years 3 months ago - 9 years 3 months ago #136748 by Deusdeorum
So the subject title pretty much says what I'd like to do, after I select a radio button I want to lock all other options so that you cannot change your answer. I have been trying below javascript to do something like that but without success, im not sure where i am thinking wrong though..
Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
    if($('#question{QID} input.radio:checked').length > 0) {
      radio.disabled = true;
    }
  });  
</script>
Last edit: 9 years 3 months ago by Deusdeorum.
The topic has been locked.
More
9 years 3 months ago #136753 by tpartner
Try this:

Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function(){ 
    $('#question{QID} input.radio').on('click', function(e){
      $('#question{QID} input.radio').not(this).prop('disabled', true);
    });
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: holch, Deusdeorum
The topic has been locked.
More
9 years 3 months ago #136754 by Deusdeorum

tpartner wrote: Try this:

Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function(){ 
    $('#question{QID} input.radio').on('click', function(e){
      $('#question{QID} input.radio').not(this).prop('disabled', true);
    });
  });
</script>


Working like a charm, as always a very nice answer Tony, cheers!
The topic has been locked.
More
9 years 3 months ago #136761 by holch
Works great in 2.5 (not tested in 2.06 yet).

Help us to help you!
  • Provide your LS version and where it is installed (own server, uni/employer, SaaS hosting, etc.).
  • Always provide a LSS file (not LSQ or LSG).
Note: I answer at this forum in my spare time, I'm not a LimeSurvey GmbH employee.
The topic has been locked.
More
9 years 3 months ago #136769 by holch
Hmmm, I think I was judging too quick. I was using the same question for another issue to test something and I noticed that when you click on the answer option instead of the radio button, the radio buttons stay the same, but the response changes...

Help us to help you!
  • Provide your LS version and where it is installed (own server, uni/employer, SaaS hosting, etc.).
  • Always provide a LSS file (not LSQ or LSG).
Note: I answer at this forum in my spare time, I'm not a LimeSurvey GmbH employee.
The topic has been locked.
More
9 years 3 months ago #136770 by Deusdeorum

holch wrote: Hmmm, I think I was judging too quick. I was using the same question for another issue to test something and I noticed that when you click on the answer option instead of the radio button, the radio buttons stay the same, but the response changes...


Not really sure what you mean, the javascript works for me even though you're clicking on the radio buttons or the corresponding text option. Version 2.06+ with a simple template
The topic has been locked.
More
9 years 3 months ago #136771 by tpartner

Hmmm, I think I was judging too quick. I was using the same question for another issue to test something and I noticed that when you click on the answer option instead of the radio button, the radio buttons stay the same, but the response changes...

Oops, forgot about the labels - try this:

Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function(){ 
    $('#question{QID} input.radio').on('click', function(e){
      $('#question{QID} input.radio').not(this).prop('disabled', true);
      $('#question{QID} input.radio:checked').prop('disabled', false);
    });
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: Ben_V
The topic has been locked.
More
9 years 3 months ago #136772 by holch
Nope, still not working under 2.5.

I actually only noticed this, because in the next question I am referring to the response of this exact question via EM.

The radio button that was marked first stays marked. But when you click on the labels, they answer seems to change, as it changes the referred response in the next question.

Help us to help you!
  • Provide your LS version and where it is installed (own server, uni/employer, SaaS hosting, etc.).
  • Always provide a LSS file (not LSQ or LSG).
Note: I answer at this forum in my spare time, I'm not a LimeSurvey GmbH employee.
The topic has been locked.
More
9 years 3 months ago #136776 by tpartner
Hmm, there seems to be a propagation of the checkconditions() function in 2.5. Try this:

Code:
<script type="text/javascript" charset="utf-8">    
  $('#question{QID} input.radio').on('click', function(e){
      $('#question{QID} input.radio').not(this).prop('disabled', true);
      $('#question{QID} input.radio:checked').prop('disabled', false).trigger('click');
    });
</script>

There may be a problem if returning to the page but my guess is if you are "locking" the radio questions after answering, you will not allow returning to the page.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
9 years 3 months ago #136783 by holch
Tested it, but now it does not do anything at all.

You can click through the answers just as if there wouldn't be any Javascript.

Help us to help you!
  • Provide your LS version and where it is installed (own server, uni/employer, SaaS hosting, etc.).
  • Always provide a LSS file (not LSQ or LSG).
Note: I answer at this forum in my spare time, I'm not a LimeSurvey GmbH employee.
The topic has been locked.
More
9 years 3 months ago #136786 by tpartner
Holch, can you please supply a sample survey?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
9 years 3 months ago - 9 years 3 months ago #136818 by holch
Hi Tpartner,

Please find my example attached.

I used the default template of 2.5 for testing (I am trying to force myself to use LS 2.5 for all new tests... ;) )


File Attachment:

File Name: limesurvey...3472.lss
File Size:14.52 KB

Help us to help you!
  • Provide your LS version and where it is installed (own server, uni/employer, SaaS hosting, etc.).
  • Always provide a LSS file (not LSQ or LSG).
Note: I answer at this forum in my spare time, I'm not a LimeSurvey GmbH employee.
Last edit: 9 years 3 months ago by holch.
The topic has been locked.
More
9 years 3 months ago - 9 years 3 months ago #136891 by tpartner
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.32 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 9 years 3 months ago by tpartner.
The topic has been locked.
More
9 years 3 months ago - 9 years 3 months ago #136897 by Deusdeorum
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: 9 years 3 months ago by Deusdeorum.
The topic has been locked.
More
9 years 3 months ago #136913 by holch
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?

Help us to help you!
  • Provide your LS version and where it is installed (own server, uni/employer, SaaS hosting, etc.).
  • Always provide a LSS file (not LSQ or LSG).
Note: I answer at this forum in my spare time, I'm not a LimeSurvey GmbH employee.
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose