Check out the LimeSurvey source code on GitHub!
Welcome, Guest
Username: Password:
  • Page:
  • 1
  • 2

TOPIC: condition based on number of answers?

condition based on number of answers? 6 years 7 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.

condition based on number of answers? 6 years 7 months ago #41827

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6574
  • Thank you received: 1477
  • Karma: 579
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.
The administrator has disabled public write access.

condition based on number of answers? 5 years 9 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.

condition based on number of answers? 5 years 9 months ago #57968

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6574
  • Thank you received: 1477
  • Karma: 579
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.
The administrator has disabled public write access.
The following user(s) said Thank You: ttophen

condition based on number of answers? 5 years 3 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.

condition based on number of answers? 5 years 3 months ago #65488

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6574
  • Thank you received: 1477
  • Karma: 579
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.
The administrator has disabled public write access.

condition based on number of answers? 5 years 3 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.

condition based on number of answers? 5 years 3 months ago #65493

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6574
  • Thank you received: 1477
  • Karma: 579
Cool. And it worked?
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

condition based on number of answers? 5 years 3 months ago #65495

  • TMSWhite
  • TMSWhite's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 758
  • Thank you received: 84
  • Karma: 37
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: 5 years 3 months ago by TMSWhite.
The administrator has disabled public write access.

condition based on number of answers? 5 years 3 months ago #65498

  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5446
  • Thank you received: 814
  • Karma: 240
@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 | [url=https://manual.limesurvey.org/Workarounds[/url]

If you found this answer helpful and it saved you some time please consider a [url=https://donate.limesurvey.org[/url] to the...
The administrator has disabled public write access.

condition based on number of answers? 5 years 3 months ago #65499

  • TMSWhite
  • TMSWhite's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 758
  • Thank you received: 84
  • Karma: 37
It will go through alpha/beta testing as soon as all the major bugs are removed, which should be quite soon. The hope/expectation is that it will be officially released sometime this Fall.
The administrator has disabled public write access.

condition based on number of answers? 5 years 3 months ago #65500

  • ttophen
  • ttophen's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 9
  • Karma: 0
Tested it several times and so far so good.
The administrator has disabled public write access.

condition based on number of answers? 5 years 3 months ago #65501

  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5446
  • Thank you received: 814
  • Karma: 240
TMSWhite wrote:
It will go through alpha/beta testing as soon as all the major bugs are removed, which should be quite soon. The hope/expectation is that it will be officially released sometime this Fall.

Of course it will need some testing. But Fall sound good. I'll expect it for winter then... ;-)
Have a look at the manual! It is a really valuable source for information. Here some helpful links:
Manual (EN) | Question Types | [url=https://manual.limesurvey.org/Workarounds[/url]

If you found this answer helpful and it saved you some time please consider a [url=https://donate.limesurvey.org[/url] to the...
The administrator has disabled public write access.

condition based on number of answers? 5 years 3 months ago #65503

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6574
  • Thank you received: 1477
  • Karma: 579
Winter here, not there :)
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

condition based on number of answers? 5 years 3 months ago #65504

  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5446
  • Thank you received: 814
  • Karma: 240
Of course, winter on the norther hemisphere. Down here we currently HAVE winter. So either I would be expecting a little too much or I really would think that the developer team is quite lazy. None of which is true.

So to be more specific: I would expect it around Christmas. ;-)
Have a look at the manual! It is a really valuable source for information. Here some helpful links:
Manual (EN) | Question Types | [url=https://manual.limesurvey.org/Workarounds[/url]

If you found this answer helpful and it saved you some time please consider a [url=https://donate.limesurvey.org[/url] to the...
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Time to create page: 0.395 seconds
Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form