Welcome, Guest
Username: Password: Remember me

TOPIC: Filter based on answer codes

Filter based on answer codes 3 years 2 months ago #60305

  • agully
  • agully's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
Hopefully someone can help me figure out a workaround for this. I have a survey where the respondent will first identify a country, and then in the next question answer some questions regarding some provinces in the identified country. Considering the number of countries in the world, creating separate conditional questions for each is a bit unwieldy.

Example:

Q1: list (dropdown)

Code Answer Option
afgh Afghanistan
alba Albania
.. ..
and so on

Q2: Multiple numeric

Code Answer Option
afgh1 Province 1
afgh2 Province 2
afgh3 Province 3
afgh4 Province 4
afgh5 Province 5
afgh6 Province 6
alba1 Province 1
alba2 Province 2
alba3 Province 3

So ideally what I envision is the respondent will select the country in Q1, and then based on that answer only the corresponding country and its provinces will appear in Q2. I'm thinking maybe with jscript one can filter based on the first four letters of the code in Q2? Maybe there is an easier way?

I tried some of these workarounds, but they appear to work by filter on answer option, which won't work in this case.

Thanks in advance
The administrator has disabled public write access.

Re: Filter based on answer codes 3 years 2 months ago #60338

  • tpartner
  • tpartner's Avatar
  • NOW ONLINE
  • LimeSurvey Team
  • Posts: 4050
  • Thank you received: 739
  • Karma: 341
Are the questions 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: Filter based on answer codes 3 years 2 months ago #60340

  • agully
  • agully's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
question-by-question survey.

I've got a workaround of sorts nearly functioning at this point.

Q1 is a dropdown as described.
Q2 is a temporary hidden table (multiple choice) that filters country names with codes that match the province list (using code from [url=hthttp://docs.limesurvey.org/tiki-index.php?page=Workarounds%3A+Manipulating+a+survey+at+runtime+using+Javascript#Filter_answers_of_a_multiple_numeric_type_question]this[/url] workaround). Automatically checks all the checkboxes.
Q3 an array with Q2 set as the filter--so now we're only looking at the provinces associated with the selected country from Q1. works but also brings up all the extraneous hidden elements from Q2.

I had to move from using 4-letter codes to incremental integers. which is fine.
Last Edit: 3 years 2 months ago by agully.
The administrator has disabled public write access.

Re: Filter based on answer codes 3 years 2 months ago #60342

  • tpartner
  • tpartner's Avatar
  • NOW ONLINE
  • LimeSurvey Team
  • Posts: 4050
  • Thank you received: 739
  • Karma: 341
Can you activate a demo?
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 based on answer codes 3 years 2 months ago #60345

  • agully
  • agully's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
unfortuately my webserver is behind a corporate firewall and cannot be accessed. maybe the lss will help. i noticed some of the subquestions are blank so maybe some data did not survive. in any case, hopefully you get the idea.

lss download
Last Edit: 3 years 2 months ago by agully.
The administrator has disabled public write access.

Re: Filter based on answer codes 3 years 2 months ago #60348

  • agully
  • agully's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
Also, the script on Q2 runs incredibly slow because in order to accommodate all of the regions it's looping through 3140 subquestions.
Last Edit: 3 years 2 months ago by agully.
The administrator has disabled public write access.

Re: Filter based on answer codes 3 years 2 months ago #60351

  • tpartner
  • tpartner's Avatar
  • NOW ONLINE
  • LimeSurvey Team
  • Posts: 4050
  • Thank you received: 739
  • Karma: 341
Well, I didn't have much time to spend on this, but this may help. It's still going to be slow because of the number of options.

Add the following script to Q2 (the checkbox question). The script gets the Q1 answer, checks all appropriate boxes and then moves to the next question.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// Get the Q1 answer
		var q1Ans = ('{INSERTANS:11777X16X193}');
		// Clean up the string
		q1Ans = q1Ans.replace(/amp; /, ' ');	
 
		// Reset all checkboxes
		$('input.checkbox').attr('checked', false);
 
		// Check all boxes with labels matching the Q1 answer
		$('label').filter( function (index) {
			return $(this).text() == q1Ans;
		}).siblings('input.checkbox').attr('checked', true);
 
		// Move to the next question
		$('form#limesurvey').submit();
 
	});
 
</script>

Then you'll want to add this to template.css to hide the checkbox question while the JavaScript and submit work:
#question193 {
	display: none;
}

You may want to add this to Q3 so the checkbox question is skipped if people go back in the survey.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
 
 
		// Skip previous question
		$('#moveprevbtn').mousedown(function () { 
			$('#thisstep').val($('#thisstep').val() - 1);
		});
 
	});
 
</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.
Moderators: ITEd
Time to create page: 0.173 seconds
Donation Image