Welcome, Guest
Username: Password: Remember me

TOPIC: Filter/disable answers depending on other questions answer

Filter/disable answers depending on other questions answer 3 years 3 months ago #64615

  • tyale
  • tyale's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 3
  • Karma: 0
Hi all, I am trying to filter some answers depending on the answers of 2 other questions but no success so far. I will try to explain below:

Q1 - Question one (multiple choice)
Answer a
Answer b
Answer c

Q2 - Question two (multiple choice)
Answer a
Answer b
Answer c

Q3 - Question three (multiple choice)
Answer a (only show if Q1a = checked)
Answer b (only show if Q1b = checked)
Answer c (only show if Q1c = checked)
Answer d (only show if Q2a = checked)
Answer e (only show if Q2b = checked)
Answer f (only show if Q2c = checked)

I saw something similar to it in here but as I am not very familiar with Javascript, I couldn't adapt it for my specific problem.

Many thanks,
Tyale
Last Edit: 3 years 3 months ago by tyale.
The administrator has disabled public write access.

Re: Filter/disable answers depending on other questions answer 3 years 3 months ago #64619

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4372
  • Thank you received: 830
  • Karma: 381
Are all questions on the same page or separate pages?
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: Filter/disable answers depending on other questions answer 3 years 3 months ago #64624

  • tyale
  • tyale's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 3
  • Karma: 0
The questions are in separated pages.
The administrator has disabled public write access.

Re: Filter/disable answers depending on other questions answer 3 years 3 months ago #64629

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4372
  • Thank you received: 830
  • Karma: 381
Okay, then the easiest way to handle it would be to load the Q1 and Q2 checked answer codes into cookies so they can be retreived in page 3 to manipulate Q3.

The following solution assumes that there is only one multiple-choice question per page and that the answer codes are identical for the various answers. In other words, these answer codes match:
- Q1a = Q3a
- Q1b = Q3b
- Q1c = Q3c
- Q2a = Q3d
- Q2b = Q3e
- Q2c = Q3f

1) Set up your survey to use JavaScript.

2) Add the following script to the source of Q1. It loads the checked answer codes for Q1 into a cookie.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		$('#movenextbtn').click(function(){
 
			var prevChecked = '';
 
			$('input.checkbox:checked').each(function(i){
				var qID = $(this).parents('div[id^="question"]').attr('id').substr(8);
				var ansID = $(this).attr('id');
				var tmp = ansID.split('X'+qID);
				var ansCode = tmp[1];
				prevChecked = prevChecked + ',' + ansCode
			});
 
			document.cookie = 'lsprevChecked1='+prevChecked+'';
 
  		});
 
	});
 
</script>

3) Add the following script to the source of Q2. It loads the checked answer codes for Q2 into a cookie.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		$('#movenextbtn').click(function(){
 
			var prevChecked = '';
 
			$('input.checkbox:checked').each(function(i){
				var qID = $(this).parents('div[id^="question"]').attr('id').substr(8);
				var ansID = $(this).attr('id');
				var tmp = ansID.split('X'+qID);
				var ansCode = tmp[1];
				prevChecked = prevChecked + ',' + ansCode
			});
 
			document.cookie = 'lsprevChecked2='+prevChecked+'';
 
  		});
 
	});
 
</script>

2) Add the following script to the source of Q3. It retrieves the answer codes and hides all answers in Q3 that don't match one.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		function getCookie ( cookieName ) {
			var results = document.cookie.match ( '(^|;) ?' + cookieName + '=([^;]*)(;|$)' );
			if ( results )
				return ( unescape ( results[2] ) );
		 	 else
				return null;
		}
 
		if($( 'input#fieldnames' ).length != 0) {
			var fieldNames = $( 'input#fieldnames' ).attr('value');
			var tmp = fieldNames.split('X');
			var sID = tmp[0];
			var gID = tmp[1];
		}
		var qID = $('div[id^="question"]:first').attr('id').substr(8);
 
		$('li[id^="javatbd"]').addClass('hiddenAns');
 
		var prevChecked = getCookie('lsprevChecked1') + getCookie('lsprevChecked2');		
		var prevArr = prevChecked.split(',');		
		$(prevArr).each(function(i){
			$('li#javatbd'+sID+'X'+gID+'X'+qID+this).removeClass('hiddenAns');
		});
 
		$('.hiddenAns').hide();
		$('.hiddenAns input.checkbox').attr('checked', false);
 
	});
 
</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: tyale

Re: Filter/disable answers depending on other questions answer 3 years 3 months ago #64661

  • tyale
  • tyale's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 3
  • Karma: 0
It worked perfect! Thank you very much!
The administrator has disabled public write access.

Re: Filter/disable answers depending on other questions answer 3 years 1 month ago #67636

  • cegreja
  • cegreja's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 25
  • Karma: 0
I have a similar question, but I guess more complex.
I have a multiple options question in one group (Q1) and 3 ranking questions (with a maximum of 3 answers to be selected from each one)in another (Q2, Q3, Q4). They all have the same answer options (car brands).

I would like to create:
1) a new array question that would have as answers the 3 answers selected from Q1, plus the three selected from Q3 and the three from Q4.
2) a new array question that would have as answers the 3 from Q2 + the 3 from Q3 + the 3 from Q4.

Since the respondent may choose the same brands in the questions, the result would range between 3 and 9 answers.

Is this possible to achieve?

Thanks again in advance to any sugestion that you could give me.
The administrator has disabled public write access.

Re: Filter/disable answers depending on other questions answer 3 years 1 month ago #67645

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5426
  • Thank you received: 307
  • Karma: 252
This is a really complex scenario. There is an advanced question setting called array filter which might help a little, but everything else requires some complex coding.

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: Filter/disable answers depending on other questions answer 3 years 1 month ago #67650

  • cegreja
  • cegreja's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 25
  • Karma: 0
I figured :( Well, I guess I can set an independent array in the group just regarding Q1. But is there a way to get the second scenario done, with the three answers from each of the ranking questions that are on the same page? Would it be simpler if instead of ranking questions I set them to multiple options instead?

Thanks a lot

ps - It would be simple if I could set several questions in the "array filter" option that you indicate, but it only allows for one, right?
The administrator has disabled public write access.

Re: Filter/disable answers depending on other questions answer 3 years 1 month ago #67653

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4372
  • Thank you received: 830
  • Karma: 381
I'm assuming that the two new array questions would be in a third (and maybe fourth) group. Correct?

If so, you could place two hidden multi-option questions in group 2 with the same answers as all others to use a filters for the subsequent arrays. So group 2 would contain 3 ranking questions and 2 multi-options questions.


SCENARIO 1 (new array #1):
The first hidden question could be used as a filter for this question. In group 2, use JavaScript to:
- Get the Q1 answers with {INSERTANS} and check those options
- Interrupt the Next/Submit function and check all options ranked in Q3 and Q4


SCENARIO 2 (new array #2):
The second hidden question could be used as a filter for this question. In group 2, use JavaScript to interrupt the Next/Submit function and check all options ranked in Q2, Q3 and Q4.


The script shouldn't be a big deal. If you can attach a sample survey I'll put something together.

The easiest way to handle the {INSERTANS} part, and have it portable (so IDs don't get screwed up on importing), would be to place a hidden <span> element in one of the questions in group 2. Something like:
<span style="display:none;">{INSERTANS:11111X22X33}</span>
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: Filter/disable answers depending on other questions answer 3 years 1 month ago #67656

  • cegreja
  • cegreja's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 25
  • Karma: 0
I have attached a sample. It's not in english, but I guess it doesn't matter. So ultimately what i'd like to achieve is (and maybe just to complicate matters a bit worse):

subquestions in Q4 = checked answers in Q1 + Q2 + Q3
subquestions in Q5 = "marcav1" + "marcav2" + "marcav3" + checked answers in Q1 + Q2 + Q3
subquestions in Q6 = "marcav1" + "marcav2" + "marcav3" + checked answers on the left scale of Q4 (it's a double array)

Answering other of your questions, i'd like the new questions to stay on the same page as it is exemplified, but if it's somehow easier to have them on different groups, no problem.

Thanks!!!


File Attachment:

File Name: limesurvey_survey_99294 1.lss
File Size: 198 KB


ps - I had to delete the subquestions in Q2 and Q3 because of the file size, but they are the same in every question.
Last Edit: 3 years 1 month ago by cegreja.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.147 seconds
Donation Image