Welcome to the LimeSurvey Community Forum

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

Radio button group per subquestion answers

  • teracomp
  • teracomp's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
7 years 5 months ago #143178 by teracomp
I'm building a survey that will have 30 questions like the one in the attached image.
Desired: The user is only allowed to select one radio button for MOST and one for LEAST.
Problem: As you can see by the screenshot, my problem is they can select either most or least from each subquestion.
If I were building this in html, I would name the radio inputs as most or least to prevent more than one selection within each group.
Is there a setting for this? Does it require additional code?

Dave Phillips
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 5 months ago #143180 by tpartner
Replied by tpartner on topic Radio button group per subquestion answers

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: teracomp
The topic has been locked.
  • teracomp
  • teracomp's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
7 years 5 months ago #143255 by teracomp
Replied by teracomp on topic Radio button group per subquestion answers
I've discovered one issue with the Array by Column question type: the user can select the same answer for both subquestions (I'm only using 2 subquestions) (see attached)

Is there a setting that prevents this for happening? Or do I need to add javascript to disable the corresponding subquestion answer?

Dave Phillips
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 5 months ago #143276 by tpartner
Replied by tpartner on topic Radio button group per subquestion answers
Hmm...it seems that there is no question validation field for that question type so I think JS is the way to go.

Adding this script to the question source should disable the corresponding radio in a row when something is clicked.

Code:
<script type="text/javascript" charset="utf-8">    
 
  $(document).ready(function() {  
 
    // Identify the questions
    var thisQuestion = $('#question{QID}');
 
    // Listener on the radios
    $('input.radio', thisQuestion).on('click', function(e) {
      $('input[type="radio"]', thisQuestion).prop('disabled', false);
      $('input.radio:checked', thisQuestion).each(function(i) {
        $(this).closest('tr.answers-list').find('input[type="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 topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 5 months ago #143278 by tpartner
Replied by tpartner on topic Radio button group per subquestion answers
...or better, this script which allows for returning to the group:

Code:
<script type="text/javascript" charset="utf-8">    
 
  $(document).ready(function() {  
 
    // Identify the questions
    var thisQuestion = $('#question{QID}');
 
    // Listener on the radios
    $('input.radio', thisQuestion).on('click', function(e) {
      $('input[type="radio"]', thisQuestion).prop('disabled', false);
      $('input.radio:checked', thisQuestion).each(function(i) {
        $(this).closest('tr.answers-list').find('input[type="radio"]').not(this).prop('disabled', true);
      });
    });
 
    // Initial settings
    $('input.radio:checked', thisQuestion).each(function(i) {
      $(this).closest('tr.answers-list').find('input[type="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: teracomp
The topic has been locked.
  • teracomp
  • teracomp's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
7 years 5 months ago #143284 by teracomp
Replied by teracomp on topic Radio button group per subquestion answers
This looks like an amazing answer...truly appreciate the script.

I'm still new enough that I don't know where to place the code. I put this in the navigator.pstpl file and it is rendered on the page...I just need to update the jQuery refs and make this work. (is this the right approach)??

Dave Phillips
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 5 months ago #143285 by Joffm
Replied by Joffm on topic Radio button group per subquestion answers
Hi, teracomp,

usually you put it into the "template.js"
Or in the question text.

Regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: teracomp
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 5 months ago #143286 by tpartner
Replied by tpartner on topic Radio button group per subquestion answers
In this case, place it in the source of the question text - manual.limesurvey.org/Workarounds:_Manip...tc..29_in_LimeSurvey

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • teracomp
  • teracomp's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
7 years 5 months ago #143327 by teracomp
Replied by teracomp on topic Radio button group per subquestion answers
Since I have 30 questions, it would be quite repetitive to put the script in each question, so I put it in template.js as the first method in the document.ready group:
Code:
  // Identify the questions
  var thisQuestion = $('[id^=question]');
  // Listener on the radios
  $('input.radio', thisQuestion).on('click', function(e) {
      alert('hi');
    $('input[type="radio"]', thisQuestion).prop('disabled', false);
    $('input.radio:checked', thisQuestion).each(function(i) {
      $(this).closest('tr.answers-list').find('input[type="radio"]').not(this).prop('disabled', true);
    });
  });
   // Initial settings
  $('input.radio:checked', thisQuestion).each(function(i) {
    $(this).closest('tr.answers-list').find('input[type="radio"]').not(this).prop('disabled', true);
  });

Since I don't have access to the {QID}, i changed the var assignment to:
var thisQuestion = $('[id^=question]');

Really appreciate the code tpartner and the hint Joffm. Fantastic support on this forum!

Would it be a better practice to have a custom.js file as part of the manifest and include any custom code like this?

Dave Phillips
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 4 months ago #143358 by tpartner
Replied by tpartner on topic Radio button group per subquestion answers
I would target that specific question type instead of any question.

Code:
$(document).ready(function() {  
 
  if($('.array-flexible-column').length > 0) {
 
    // Identify the question
    var thisQuestion = $('.array-flexible-column:eq(0)');
 
    // Listener on the radios
    $('input.radio', thisQuestion).on('click', function(e) {
      $('input[type="radio"]', thisQuestion).prop('disabled', false);
      $('input.radio:checked', thisQuestion).each(function(i) {
        $(this).closest('tr.answers-list').find('input[type="radio"]').not(this).prop('disabled', true);
      });
    });
 
    // Initial settings
    $('input.radio:checked', thisQuestion).each(function(i) {
      $(this).closest('tr.answers-list').find('input[type="radio"]').not(this).prop('disabled', true);
    });
  }
});

The only justification for a custom.js file would be if you wanted to use this template for several surveys with different JS requirements.

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: teracomp
The topic has been locked.
  • teracomp
  • teracomp's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
7 years 4 months ago #143364 by teracomp
Replied by teracomp on topic Radio button group per subquestion answers
Once again, i greatly appreciate the advice and wise counsel. These posts are of great value to me as I get deeper into LimeSurvey and learn the specifics.

In this case, I see the .array-flexible-column class as the target element rather than my solution which relied on having just one question per page where I knew [id^=question] would work. Your solution appears to be more relevant.

The primary thought behind custom.js as a resource is establishing an architecture I can use for any survey. I often use this approach with stylesheets, e.g., custom.css, so i can quickly isolate styles I've added that override the base theme/template. I hear your recommendation and for the time being, I'll keep everything in template.js as I learn more.

Thanks again for taking time to review this post.

Dave Phillips
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose