Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: Filtering some answers from an array

Filtering some answers from an array 3 years 8 months ago #56513

  • vadindot
  • vadindot's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 52
  • Thank you received: 1
  • Karma: 0
Okay here is my example survey...

Q1 is a multiple options question
Q2 is an array

We want subquestion #4 or Q2 to only be displayed if Q1 has option 4 or option 5 checked (or both)

My guess if that I need to create a hidden question with 4 options (there are 4 subquestions Q2), generate a checkmark in each of the first 3 options because they are always displayed, and then do something to check the fourth option if 4 or 5 is checked in Q1... so far I havn't been able to figure out how to do that...
Attachments:
The administrator has disabled public write access.

Re: Filtering some answers from an array 3 years 8 months ago #56563

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5348
  • Thank you received: 298
  • Karma: 250
conditions can only be applied to whle questions. Set a condition to only display Q2 if Q1 was answered option4 OR option5.
Then you can also use the question attribute array_filter to display only those options at Q2 that haven been chosen before.

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.

Re: Filtering some answers from an array 3 years 8 months ago #56661

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4293
  • Thank you received: 810
  • Karma: 373
1) In group 1, create a multiple-options with the same answer codes as the array on group 2 (we'll hide this with JavaScript).

2) In the group 2 array, set the "Array filter" dependent on the hidden question.

3) Test the array filter.

4) Set up your survey to use JavaScript and place the following script in the source of the hidden question. Replace "QQ" (line 5) with the ID of the first multiple-options question in group 1, and replace "HH" (line 6) with the ID of the hidden question.

The script checks all options of the hidden question except #4. Option 4 of the hidden question is only checked if options 4 or 5 of the first question are checked. The array filter then displays the rows of the group 2 array accordingly.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		var q1 = QQ;
		var qHidden = HH;
 
		$('#question'+qHidden+'').hide();
 
		$('form#limesurvey').submit(function(){
 
			// Reset the hidden question
			$('#question'+qHidden+' input.checkbox').attr('checked', true);
			$('#question'+qHidden+' input.checkbox:eq(3)').attr('checked', false);
 
			if($('#question'+q1+' input.checkbox:eq(3)').attr('checked') == true || $('#question'+q1+' input.checkbox:eq(4)').attr('checked') == true) {
				$('#question'+qHidden+' input.checkbox:eq(3)').attr('checked', true);
			}
 
			// Carry on with submit
			return true;
		});		
 
	})
 
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.
The following user(s) said Thank You: vadindot

Re: Filtering some answers from an array 3 years 8 months ago #56680

  • vadindot
  • vadindot's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 52
  • Thank you received: 1
  • Karma: 0
As usual, thanks for coming to the rescue Tony...

I've got one more for you that is similar.

Q1 is list(radio) with 18 options.

I need to create a hidden variable where I can check one or more items based on which option is listed in Q1.

For example,
For option 1 in Q1 I want to check 2,3 in "qHidden"
For option 2 in Q1 I want to check 1,2,3 in "qHidden"
etc...

Can you help me get a basic script together for something like this?

Thanks in advance
The administrator has disabled public write access.

Re: Filtering some answers from an array 3 years 8 months ago #56686

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4293
  • Thank you received: 810
  • Karma: 373
Is qHidden a radio or checkbox question?
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 3 years 8 months ago by tpartner.
The administrator has disabled public write access.

Re: Filtering some answers from an array 3 years 8 months ago #56687

  • vadindot
  • vadindot's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 52
  • Thank you received: 1
  • Karma: 0
checkbox
The administrator has disabled public write access.

Re: Filtering some answers from an array 3 years 8 months ago #56723

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4293
  • Thank you received: 810
  • Karma: 373
I think the easiest way to make this more or less human readable is to assign classes to the radio and checkbox inputs. Then we can interrupt the next/submit function, find which radio is selected and check the appropriate checkboxes in the hidden question.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		// The question IDs
		var q1 = QQ;
		var qHidden = HH;
 
		// Hide the hidden question
		$('#question'+qHidden+'').hide();
 
		// Assign classes to the inputs
		$('#question'+q1+' input.radio').each(function(i) {
			$(this).addClass('q1Row'+(i+1)+'');
		});
		$('#question'+qHidden+' input.checkbox').each(function(i) {
			$(this).addClass('qHiddenRow'+(i+1)+'');
		});
 
		// Interrupt the Next/Submit function
		$('form#limesurvey').submit(function(){
 
			// Reset the hidden question
			$('#question'+qHidden+' input.checkbox').attr('checked', false);
 
			// If q1 row 1 is checked we do this
			if($('.q1Row1').attr('checked') == true) {
				$('.qHiddenRow2').attr('checked', true);
				$('.qHiddenRow3').attr('checked', true);
			}
			// If q1 row 2 is checked we do this
			if($('.q1Row2').attr('checked') == true) {
				$('.qHiddenRow1').attr('checked', true);
				$('.qHiddenRow2').attr('checked', true);
				$('.qHiddenRow3').attr('checked', true);
			}
			// Add more IF statements as required...
 
			// Carry on with submit
			return true;
		});		
 
	})
 
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 3 years 8 months ago by tpartner.
The administrator has disabled public write access.

Re: Filtering some answers from an array 3 years 8 months ago #56764

  • vadindot
  • vadindot's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 52
  • Thank you received: 1
  • Karma: 0
once again let me say Tony Rocks!

thanks, both these worked great
The administrator has disabled public write access.

Re: Filtering some answers from an array 3 years 8 months ago #56770

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5348
  • Thank you received: 298
  • Karma: 250
We need to set up a website like "www.iowetonyabeer.com" where everybody can sign in and volunteer for paying for a beer. Once we find a team member in Canada we can then ask if he has a truck to deliver all this beer sometime.

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.

Re: Filtering some answers from an array 3 years 8 months ago #56773

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4293
  • Thank you received: 810
  • Karma: 373
:laugh:
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.141 seconds
Donation Image