Welcome, Guest
Username: Password: Remember me

TOPIC: Array filter of the answers of 2 questions

Array filter of the answers of 2 questions 1 month 3 weeks ago #111368

I have 3 questions in my survey. The type of these questions is multiple choice.

Q1. What animals do you know?
1. A cat
2. A dog
3. A monkey
4. A pig
5. An ekhtiander
6. A fox

Q2. Do you know animals such as..? [Array exclusion filter of Q1]
1. A cat
2. A dog
3. A monkey
4. A pig
5. An ekhtiander
6. A fox

Q3. What animals have you ever had?
[The available variants of the answers must be from Q1 and Q2 together]

I would like to variants of answers will be all answers of Q1 and Q2 together.
Array filter in Q3 allows me to choice one question only.

How can I do it?

I think maybe I should remove array exclusion filter in Q2 and insert JavaScript code to Q2 that the answers of Q1 were copied to Q2 and I should set array filter to Q3 value Q2. But I can't write this JavaScript code.

Do you have any solutions?

P.S. I am sorry for my English.
Last Edit: 1 month 3 weeks ago by limeuser2014.
The administrator has disabled public write access.

Array filter of the answers of 2 questions 1 month 3 weeks ago #111436

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4206
  • Thank you received: 790
  • Karma: 360
The array filter works like this:

1) If the question codes in all filter questions are the same, the option must be selected in ALL filter questions to appear in the filtered array.

2) If the question codes in the filter questions unique, the option must be selected in ONE filter questions to appear in the filtered array (Q3).

So, a simple solution for you may be to give the options in Q1 and Q2 unique codes and then "double up" the array rows with matching unique codes. Filter Q3 with both Q1 and Q2.

Of course, you will need to handle these doubled sub-questions in post survey analysis.

Q1. What animals do you know?
1. A cat
2. A dog
3. A monkey
4. A pig
5. An ekhtiander
6. A fox

Q2. Do you know animals such as..? [Array exclusion filter of Q1]
7. A cat
8. A dog
9. A monkey
10. A pig
11. An ekhtiander
12. A fox

Q3. What animals have you ever had?
1. A cat
2. A dog
3. A monkey
4. A pig
5. An ekhtiander
6. A fox
7. A cat
8. A dog
9. A monkey
10. A pig
11. An ekhtiander
12. A fox


.
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.

Array filter of the answers of 2 questions 1 month 3 weeks ago #111453

tpartner, thank you for reply.

I done it and Q3 works as I wanted.

But now array exclusion filter on Q2 (excluding Q1 answers) don't work.

How can I hide Q1 answers in Q2?
The administrator has disabled public write access.

Array filter of the answers of 2 questions 1 month 3 weeks ago #111455

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4206
  • Thank you received: 790
  • Karma: 360
Oh, yeah, I zoned on that. You'll need to use JavaScript to handle that.

Assuming you are using LS 2.05 and Q1 and Q2 are on the same page and are sequential, adding this to the source of Q1 should do the trick.

<script type="text/javascript" charset="utf-8">	
    $(document).ready(function(){
 
		// Identify the questions
		var q1= $('#question{QID}');
		var q2 = $(q1).nextAll('.multiple-opt:eq(0)');
 
		// Initial states
		$('input.checkbox:checked', q1).each(function(i){
			var thisIndex = $(this).closest('.question-item').index();
			var q2Item = $('.question-item:eq('+thisIndex+')', q2);
			$(q2Item).hide();
		});
 
		// Listener on Q1
		$('input.checkbox', q1).change(function(event){
			var thisIndex = $(this).closest('.question-item').index();
			var q2Item = $('.question-item:eq('+thisIndex+')', q2);
			var q2Input = $('input.checkbox', q2Item);
			if($(this).is(':checked')) {
				$(q2Input).prop('checked', false);
				$('input[type="hidden"]', q2Item).val('');
				$(q2Item).hide();
				checkconditions($(q2Input).attr('value'), $(q2Input).attr('name'), $(q2Input).attr('type'))
			}
			else {
				$(q2Item).show();
			}
		});
	});
</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: limeuser2014

Array filter of the answers of 2 questions 1 month 3 weeks ago #111458

Tony, thank you for you help.

I attached the working example for someone.
Attachments:
The administrator has disabled public write access.

Array filter of the answers of 2 questions 1 month 2 weeks ago #111626

  • srocky
  • srocky's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 4
  • Karma: 0
Can I ask one more question about this solution?

Is it possible to set by JavaScript that one of options (ex. "non of above") will always be visible?

I have two questions Q1 and Q2, and every of them have exclusion option: Non of above. But when someone check this option in Q1 it disappear in Q2.

Please help.

(sorry for my English)
The administrator has disabled public write access.

Array filter of the answers of 2 questions 1 month 2 weeks ago #111632

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4206
  • Thank you received: 790
  • Karma: 360
Try changing this line:
if($(this).is(':checked')) {
To this:
if($(this).is(':checked') && (thisIndex+1) != $('.question-item', q1).length) {

That should prevent filtering on the last items.


.
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.

Array filter of the answers of 2 questions 1 month 2 weeks ago #111635

  • srocky
  • srocky's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 4
  • Karma: 0
It works :) Thank you a lot!
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.121 seconds
Donation Image