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

TOPIC: condition based on number of answers?

condition based on number of answers? 4 years 2 months ago #41823

  • beckers
  • beckers's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 2
  • Karma: 0
Hi,

I want to set the condition for a question based on the number of selected answers in a multiple options question.

For example:

question1 Select the fruits you like: (multiple options)
- apples
- oranges
- limes

ask question 2 when at least 2 options in question 1 are selected

Is this possible?
The administrator has disabled public write access.

Re:condition based on number of answers? 4 years 2 months ago #41827

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4054
  • Thank you received: 741
  • Karma: 341
Well, you could do it using condition scenarios but it would get a little onerous with more than 3 or 4 options in Q1:

Show Q2 if:

Q1=apples && Q1=oranges
OR
Q1=apples && Q1=limes
OR
Q1=oranges && Q1=limes

If you have many options in Q1, it may be easier to put JavaScript listeners on the checkboxes of Q1 to show/hide Q2 depending on the number checked.
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.

Re: condition based on number of answers? 3 years 4 months ago #57964

  • JoyLaneResearch
  • JoyLaneResearch's Avatar
I need to do it with ten fruits.

Q1 - How many fruits do you like?
Apples, Bananas, Clementines, Dates, ...

Q2 (IF MORE THAN ONE FRUIT CHOSEN) - Which is your favorite?

Any (practical) suggestions?

Thanks!
The administrator has disabled public write access.

Re: condition based on number of answers? 3 years 4 months ago #57968

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4054
  • Thank you received: 741
  • Karma: 341
This should do the trick if all questions are on the same page.

1) Create the first multi-opts question (Q1).

2) Create a short-text question (we'll call it qHidden and hide it with JavaScript later).

3) Create your conditional question (Q2) and make it conditional on qHidden = 1.

4) Add the following script to the souce of one of the questions or help text. Replace the following parameters in line 6:
- 11 = your Q1 ID
- 22 = your Q2 ID
- HH = your qHidden ID
- CC = The maximum number of checked boxes in Q1 BEFORE Q2 appears
<script type="text/javascript" charset="utf-8">
	$(document).ready(function() {
 
		// Call the function
		// Params - Q1 ID, Q2 ID, Hidden question ID, max number of checked BEFORE Q2 shows 
		countChecked(11, 22, HH, CC);
 
		function countChecked(q1ID, q2ID, qHiddenID, count) {
 
			// Hide the hidden question
			$('#question'+qHiddenID+'').hide();
 
			// A listener on Q1 checkboxes
			$('#question'+q1ID+' input.checkbox').click(function () { 
 
				// Count the checked boxes and populate the hidden question accordingly
				if ($('#question'+q1ID+' input.checkbox:checked').length > count) {
					$('#question'+qHiddenID+' input.text').val(1);
				} 
				else {
					$('#question'+qHiddenID+' input.text').val(0);
				}
 
				// Fire the conditions function
				var hiddenInputValue = $('#question'+qHiddenID+' input.text').val();
				var hiddenInputName = $('#question'+qHiddenID+' input.text').attr('name');
				var hiddenInputType = $('#question'+qHiddenID+' input.text').attr('type');
				checkconditions(hiddenInputValue, hiddenInputName, hiddenInputType)
			});
		}
 
	});
 
</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: ttophen

Re: condition based on number of answers? 2 years 10 months ago #65485

  • ttophen
  • ttophen's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 9
  • Karma: 0
Is there a way to do this question by question?
The administrator has disabled public write access.

Re: condition based on number of answers? 2 years 10 months ago #65488

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4054
  • Thank you received: 741
  • Karma: 341
Not with the JavaScript solution. The multiple-options and the hidden question need to be on the same page.
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.

Re: condition based on number of answers? 2 years 10 months ago #65492

  • ttophen
  • ttophen's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 9
  • Karma: 0
Thanks for the quick response. I was able to place each question in its own group so it still displayed 1 question on a page and then I placed the multi-option question and the hidden in it's own group.
The administrator has disabled public write access.

Re: condition based on number of answers? 2 years 10 months ago #65493

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4054
  • Thank you received: 741
  • Karma: 341
Cool. And it worked?
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.

Re: condition based on number of answers? 2 years 10 months ago #65495

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
This will be much easier in LimeSurvey 1.92, which uses ExpressionManager.

Say your question about fruit is Q1, a "Multiple Choice Checkbox" type question (type M), with sub-questions A-J (for 10 different fruit).

The relevance for your follow-up question would be:
sum(Q1_A=="Y", Q1_B=="Y", ..., Q1_F=="Y") > 1

And the question could be:
You said you liked the following fruit:  {implode(", ", Q1_A, Q1_B, Q1_C, ... Q1_F)}.  Which do you like best?

The same strategy would work if you asked the questions across several pages - you'd just have different variable names.

This also works on the same page - so you could have the text of Q2 be dynamically updated as people change their answers to Q1.
Last Edit: 2 years 10 months ago by TMSWhite.
The administrator has disabled public write access.

Re: condition based on number of answers? 2 years 10 months ago #65498

  • holch
  • holch's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 2549
  • Thank you received: 285
  • Karma: 102
@TMSWhite: Any prevision when LS 1.92 will come out?
Have a look at the manual! It is a really valuable source for information. Here some helpful links:
Manual (EN) | Question Types | Question Attributes | Workarounds

If you found this answer helpful and it saved you some time please consider a donation to the project to keep Limesurvey going!
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.253 seconds
Donation Image