Prefill multiple choice question from array

More
3 years 1 week ago #114464 by granen
granen created the topic: Prefill multiple choice question from array
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

Please Log in to join the conversation.

More
3 years 1 week ago #114470 by Mazi
Mazi replied the topic: Prefill multiple choice question from array
Please post a link to an activated test survey.


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
3 years 1 week ago #114484 by granen
granen replied the 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

Please Log in to join the conversation.

More
3 years 6 days ago - 3 years 6 days ago #114500 by tpartner
tpartner replied the 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:

<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: 3 years 6 days ago by tpartner.

Please Log in to join the conversation.

More
3 years 4 days ago #114538 by granen
granen replied the 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?

Please Log in to join the conversation.

More
3 years 4 days ago #114539 by Mazi
Mazi replied the topic: Prefill multiple choice question from array

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

Yep!


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
3 years 3 days ago #114624 by tpartner
tpartner replied the 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.

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!