Welcome to the LimeSurvey Community Forum

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

Prefill multiple choice question from array

  • granen
  • granen's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 4 months ago #114464 by granen
I am trying to prefill a multiple choice question from an array question. I need to do this to be able to in a second steg use array filter and only show alternatives based on the answer in the first array question.

I have tried a javascript solution, is this my best option? My script is shown below, but I cant get it working. If they answer alternative 1 or 2 in the first question in the array the first box in the multiple choice should be checked. See the code below, am I doing something wrong?


<script type="text/javascript" charset="utf-8">

$(document).ready(function() {

var array1 = '{INSERTANS:252845X737X139931}';

if(array1 == 'alternative 1') {
$('#answer252845X738X153931').attr('checked', true);
}
if(array1 == 'alternative 2') {
$('#answer252845X738X153931').attr('checked', true);
}


</script>



1. Array 1
252845X737X13993

2. Multiple choice
252845X738X15393
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
9 years 4 months ago #114470 by Mazi
Please post a link to an activated test survey.

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
  • granen
  • granen's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 4 months ago #114484 by granen
Replied by granen on topic Prefill multiple choice question from array
Hi Mazi,

It is a new version, so the QID above is not the same.

ls.granens.se/Limesurvey/index.php/surve...66/newtest/Y/lang/sv
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 4 months ago - 9 years 4 months ago #114500 by tpartner
Replied by tpartner on topic Prefill multiple choice question from array
Judging by the jQuery version, you'll need to use .prop('checked') instead of .attr('checked'). And using Expression Manager and question codes is simpler than using INSERTANS.

So, for example if the array has a question code of "q1" and the sub-question codes (y-axis) are 1, 2, 3 and 4, adding something like this to the source of the multiple-choice question should do the trick:

Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function(){
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Get the Q1 answers
    var q1Row1 = '{q1_1.NAOK}';
    var q1Row2 = '{q1_2.NAOK}';
    var q1Row3 = '{q1_3.NAOK}';
    var q1Row4 = '{q1_4.NAOK}';
 
    // Check the appropriate boxes
    if(q1Row1 == 1 || q1Row1 == 2) {
      $('input.checkbox:eq(0)', thisQuestion).prop('checked', true);
      $('input.checkbox:eq(0)', thisQuestion).parent().find('input[type="hidden"]').val('Y');
    }
    if(q1Row2 == 1 || q1Row2 == 2) {
      $('input.checkbox:eq(1)', thisQuestion).prop('checked', true);
      $('input.checkbox:eq(1)', thisQuestion).parent().find('input[type="hidden"]').val('Y');
    }
    if(q1Row3 == 1 || q1Row3 == 2) {
      $('input.checkbox:eq(2)', thisQuestion).prop('checked', true);
      $('input.checkbox:eq(2)', thisQuestion).parent().find('input[type="hidden"]').val('Y');
    }
    if(q1Row4 == 1 || q1Row4 == 2) {
      $('input.checkbox:eq(3)', thisQuestion).prop('checked', true);
      $('input.checkbox:eq(3)', thisQuestion).parent().find('input[type="hidden"]').val('Y');
    }
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 9 years 4 months ago by tpartner.
The topic has been locked.
  • granen
  • granen's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 4 months ago #114538 by granen
Replied by granen on topic Prefill multiple choice question from array
Hi tpartner,

A huge thanks for you help. Your solution looks much easier than the one I started with and it works!

Does the following choose the first checkbox?
('input.checkbox:eq(0)

Is it possible to have this java question on the same page as a second question with a filter on the java question? Right now the question with a filter doesnt work until you reload the page or "check and uncheck" a question. Is there any tricks to force the array filter to work?
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
9 years 4 months ago #114539 by Mazi

granen wrote: Does the following choose the first checkbox?
('input.checkbox:eq(0)

Yep!

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 4 months ago #114624 by tpartner
Replied by tpartner on topic Prefill multiple choice question from array

Is it possible to have this java question on the same page as a second question...

I'm sorry, I don't understand. What is a "java" question?

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