Welcome to the LimeSurvey Community Forum

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

How to add a button to make the same selection for all questions

  • lintu
  • lintu's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 years 1 month ago #159057 by lintu
Hello Folks,

I have a survey with 90 questions with each having 5 answers with radio buttons for making a selection. I would like to place a button at the very top of the survey and make it easy for the survey takers to make the same selection (pick 1 radio button out of the 5) for the 90 questions.

Is this possible with LimeSurvey and if so, please guide me on how to achieve this. Thanks in advance for your help.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 1 month ago #159062 by Joffm
Hello, lintu,

my quick and dirty solution:

Place a first question - equal to the others.
With a - very long - equation put the answer to this question into all others.

Best regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • lintu
  • lintu's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 years 1 month ago #159064 by lintu
Hi Joffm,

Thanks for your quick response. Will your suggestion work if I have the questions in multiple pages (as part of different groups)?
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 1 month ago - 7 years 1 month ago #159067 by Joffm
Hi, lintu
{Q2=if(Q1==1,1,if(Q1==2,2,3))} works if equation and Q2 are in one group and Q1 is in a different group.

The other way - just try.

If not: Set an equation at the beginning of each group.

Best regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 7 years 1 month ago by Joffm.
The topic has been locked.
  • lintu
  • lintu's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 years 1 month ago - 7 years 1 month ago #159111 by lintu
I used the following code within the question source and it worked. I am able to use the selection for a question answer to set the remaining questions radio button choices:
Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function(){
 
     // Find the checked option for question code G2Q1N
     var qc = '{G2Q1N}';
 
    if(qc == "1"){
        document.getElementById('answer{SID}X{GID}X{QID}SQ001-A1').checked = true;   
    }else if(qc == "2"){
        document.getElementById("answer{SID}X{GID}X{QID}SQ001-A2").checked = true;      
    }
        else if(qc == "3"){
        document.getElementById("answer{SID}X{GID}X{QID}SQ001-A3").checked = true;    
    }
        else if(qc == "4"){
        document.getElementById("answer{SID}X{GID}X{QID}SQ001-A4").checked = true;   
    }
        else if(qc == "5"){
        document.getElementById("answer{SID}X{GID}X{QID}SQ001-A5").checked = true;     
    }
});
</script>
 

I would hate to copy/paste this code in 90+ questions, so I added it to the script/template.js file. But the code does not work there - it is not able to find the selection for question code G2Q1N and gives a 'Undefined variable' error. What is the proper way to find the selected choose for the radio button on question code G2Q1N?

Thanks!!
Last edit: 7 years 1 month ago by lintu.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 1 month ago #159116 by tpartner
You cannot access Expression Manager variables directly from template.js. I suppose you could place that script in question.pstpl but you would need to add some sort of condition to ensure it only affects the appropriate questions.

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 1 month ago #159117 by tpartner
...maybe adding a question class to the appropriate questions and then something like this:

Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function(){
    if($('#question{QID}').hasClass('my-class') {
       // Find the checked option for question code G2Q1N
       var qc = '{G2Q1N}';
 
      if(qc == "1"){
        document.getElementById('answer{SID}X{GID}X{QID}SQ001-A1').checked = true;   
      }else if(qc == "2"){
        document.getElementById("answer{SID}X{GID}X{QID}SQ001-A2").checked = true;      
      }
        else if(qc == "3"){
        document.getElementById("answer{SID}X{GID}X{QID}SQ001-A3").checked = true;    
      }
        else if(qc == "4"){
        document.getElementById("answer{SID}X{GID}X{QID}SQ001-A4").checked = true;   
      }
        else if(qc == "5"){
        document.getElementById("answer{SID}X{GID}X{QID}SQ001-A5").checked = 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 1 month ago #159118 by tpartner
...and, maybe this to shorten it a bit...

Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function(){
    if($('#question{QID}').hasClass('my-class') {
       // Find the checked option for question code G2Q1N
       var qc = '{G2Q1N}';
 
       // Check the corresponding option here
       $('#answer{SID}X{GID}X{QID}SQ001-A'+qc).prop('checked', 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.
  • lintu
  • lintu's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 years 1 month ago - 7 years 1 month ago #159119 by lintu
Hello Tony,

Thank you very much for helping me out with a code snippet. I checked the LimeSurvey manual and found the full list of question classes. The class for Array (5 point choice) is '.array-5-pt' which I used in the code below:
Code:
<script type="text/javascript" charset="utf-8">
    $(document).ready(function(){
      if($('#question{QID}').hasClass('.array-5-pt') {
       // Find the checked option for question code Q1
       var qc = '{Q1}';
 
       // Check the corresponding option here
       $('#answer{SID}X{GID}X{QID}SQ001-A'+qc).prop('checked', true);
    }
  });
</script>

I inserted this piece of code at the beginning of question.pstpl but it's not working - I'll keep looking into what's wrong. I have attached my test survey and test template to this post if you want to try it out on your end.
Last edit: 7 years 1 month ago by lintu.
The topic has been locked.
  • lintu
  • lintu's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 years 1 month ago - 7 years 1 month ago #159120 by lintu
A quick update: I found that my original code below works by placing it in template/question.pstpl if the question type is '5 point choice' but does not work when it's an 'array'. It is throwing an "undefined variable" error on the question code. How do I resolve this?
Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function(){
 
     // Find the checked option for question code G2Q1N
     var qc = '{G2Q1N}';
 
    if(qc == "1"){
        document.getElementById('answer{SID}X{GID}X{QID}SQ001-A1').checked = true;   
    }else if(qc == "2"){
        document.getElementById("answer{SID}X{GID}X{QID}SQ001-A2").checked = true;      
    }
        else if(qc == "3"){
        document.getElementById("answer{SID}X{GID}X{QID}SQ001-A3").checked = true;    
    }
        else if(qc == "4"){
        document.getElementById("answer{SID}X{GID}X{QID}SQ001-A4").checked = true;   
    }
        else if(qc == "5"){
        document.getElementById("answer{SID}X{GID}X{QID}SQ001-A5").checked = true;     
    }
});
</script>
 

Thanks
Last edit: 7 years 1 month ago by lintu.
The topic has been locked.
  • lintu
  • lintu's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
7 years 1 month ago #159121 by lintu
Solved the problem by changing var qc = '{G2Q1N}'; to var qc = '{G2Q1N_SQ001}';
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose