Welcome, Guest
Username: Password:

TOPIC: Using previous Array question responses to set following Array subquestions

Using previous Array question responses to set following Array subquestions 3 years 1 week ago #96318

  • tpartner
  • tpartner's Avatar
  • Online
  • LimeSurvey Team
  • Posts: 5918
  • Thank you received: 1298
  • Karma: 522
I've updated the script to include the following features:
- Automatically identify the questions as long as the script is placed in the source of the hidden question and that question is between the two arrays
- Assign column-specific classes to the first array to make it easier to identify the "trigger" columns
- Aggregate the listener code into a common function
- Fix up the q2 row background colours after firing the check conditions function

<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() { 
 
		// Call the function
		countChecked('{SGQ}');
 
		function countChecked(sgq) {
			// Identify the questions
			var sID = sgq.split('X')[0];
			var gID = sgq.split('X')[1];
			var qHiddenID = sgq.split('X')[2];
			var qHidden = $('#question'+qHiddenID+'');
			if($(qHidden).length > 0) {
				var q1 = $(qHidden).prevAll('.array-flexible-row:eq(0)'); 
				var q1ID = $(q1).attr('id').split('question')[1];
				var q2 = $(qHidden).nextAll('.array-flexible-row:eq(0)'); 
				var q2ID = $(q2).attr('id').split('question')[1];
			}
 
			// Hide the hidden question
			$('#question'+qHiddenID+'').hide();
 
			// Assign column-specific classes to Q1
			$('.answers-list', q1).each(function(i){
				$('td.answer-item', this).each(function(i){
					$(this).addClass('answer-column-'+(i+1)+'');
				});
			});
 
			// A listener on the 1st column of Q1 radio buttons 
			$('.answer-column-1', q1).click(function () { 
				handleHiddenInput (this, 'unchecked');
			});
 
			// A listener on the 2nd and 3rd columns of Q1 radio buttons 
			$('.answer-column-2, .answer-column-3', q1).click(function () {
				handleHiddenInput (this, 'checked');
			});
 
			// A function to toggle the hidden checkboxes 
			function handleHiddenInput(trigger, state) {
				// Identify some stuff
				var rowID = $(trigger).closest('tr.answers-list').attr('id');
				var tmp = rowID.split('X'+q1ID);
				var answerCode = tmp[1];
				var hiddenInput = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode); 
 
				// Toggle the input depending on "state"
				var checkedState = false;
				if(state == 'checked') {
					checkedState = true;
				}
				$(hiddenInput).attr('checked', checkedState);
 
				// Fire the conditions function to hide the corresponding row in Q2
				var hiddenInputValue = $(hiddenInput).attr('value');
				var hiddenInputName = $(hiddenInput).attr('name');
				var hiddenInputType = $(hiddenInput).attr('type');
				checkconditions(hiddenInputValue, hiddenInputName, hiddenInputType);
 
				// Fix up the q2 row background colours
				$('.answers-list:visible', q2).each(function(i, el){
					$(el).removeClass('array1, array2');
					if(i % 2 == 0) {
						$(el).addClass('array2');
					}
					else {
						$(el).addClass('array1');
					}
				});
			}
		}
 
	});
</script>

Here is the survey back with the updated code in the hidden question. I added relevance on the second array to only show that question if at least one check-box in qHidden is checked.
count(that.qHidden.NAOK)>0

File Attachment:

File Name: limesurvey...5531.lss
File Size:25 KB
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 3 years 1 week ago by tpartner.
The administrator has disabled public write access.
The following user(s) said Thank You: StuTheQ

Using previous Array question responses to set following Array subquestions 3 years 1 week ago #96393

  • StuTheQ
  • StuTheQ's Avatar
  • Offline
  • Junior Lime
  • Posts: 26
  • Karma: 0
Dear Tony,

First of all, a big "Thank you!" for, once again, helping me out.

Your "turnkey" solution works perfectly. To be honest, I was expecting JavaScript code that closely resembled the original workaround that you posted back in 2011 (post #58058) but clearly things have moved on substantially since then. I spent yesterday going through the new workaround script, trying to understand the role of the new functions (i.e. trying to advance my infinitesimally small knowledge of JavaScript!) and also playing with the code so that I can change around the "trigger" columns in Q1.

If I understand correctly:

The workaround can be used as is (i.e. no need to specify or update question IDs) whenever I want to use an Array Q(n) to set an Array Q(n+1), provided that the workaround script given is placed in the source of the hidden question inserted between the two arrays.

(Out of curiosity, but please don't answer these if you've got better things to do!:
  • 'Relevance' now definitively supersedes 'conditions'?
  • What is the exact role of the qHidden toggle function?
  • The 'Fix up the q2 row background colours' script ensures that the subquestion rows in Q2 are alternate colours, regardless of the subquestion rows checked in Q1?)

By the way, I saw that this topic has received 22k views! Is that something that I can be proud of??!! :cheer:

Thanks again, Tony!

Regards,

StuTheQ
The administrator has disabled public write access.

Using previous Array question responses to set following Array subquestions 3 years 1 week ago #96394

  • tpartner
  • tpartner's Avatar
  • Online
  • LimeSurvey Team
  • Posts: 5918
  • Thank you received: 1298
  • Karma: 522
'Relevance' now definitively supersedes 'conditions'?
It doesn't really supersede but is an alternative. If you use the conditions designer, a relevance rule will automatically be generated and inserted for you. Alternatively, you can write your own relevance rule with far more flexibility than the conditions designer allows. See manual.limesurvey.org/Expression_Manager...ons_and_Relevance.3F.

What is the exact role of the qHidden toggle function?
I'm not sure I understand the question. The handleHiddenInput() function checks or unchecks boxes in the hidden question as you click radios in the first array. The array filter then shows or hides the corresponding rows in the second array.

The 'Fix up the q2 row background colours' script ensures that the subquestion rows in Q2 are alternate colours, regardless of the subquestion rows checked in Q1?
Correct. When rows are shown or hidden, it just removes all of the array1 and array2 classes from the rows and then reapplies them sequentially to the visible rows.
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 3 years 1 week ago by tpartner.
The administrator has disabled public write access.
The following user(s) said Thank You: StuTheQ

Using previous Array question responses to set following Array subquestions 3 years 1 week ago #96397

  • StuTheQ
  • StuTheQ's Avatar
  • Offline
  • Junior Lime
  • Posts: 26
  • Karma: 0
Hi again Tony,

Thanks for the useful replies (sorry to pester you :angry: ; it's just that I always like to understand stuff, if possible! But I know that's not what the forum is for... :blush: ).
It doesn't really supersede but is an alternative. If you use the conditions designer, a relevance rule will automatically be generated and inserted for you. Alternatively, you can write your own relevance rule with far more flexibility than the conditions designer allows. See manual.limesurvey.org/Expression_Ma...ons_and_Relevance.3F.
Yes, I'd read that; the 'best practice' approach, perhaps? (no reply needed!)

What is the exact role of the qHidden toggle function?
I'm not sure I understand the question. The handleHiddenInput() function checks or unchecks boxes in the hidden question as you click radios in the first array. The array filter then shows or hides the corresponding rows in the second array.
Yes, I'd understood that function from the original workaround. It was just the "new" (?) 'toggle' function script which intrigued me (some kind of 'reset' function between iterations?) (no reply needed!)

The 'Fix up the q2 row background colours' script ensures that the subquestion rows in Q2 are alternate colours, regardless of the subquestion rows checked in Q1?
Correct. When rows are shown or hidden, it just removes all of the array1 and array2 classes from the rows and then reapplies them sequentially to the visible rows.
Cool!! :)

Guess it's about time that I enrolled for JavaScript evening classes somewhere!

Thanks, Tony.

Regards,

Stu
The administrator has disabled public write access.

Using previous Array question responses to set following Array subquestions 2 years 11 months ago #97476

  • arielmosto
  • arielmosto's Avatar
  • Offline
  • Junior Lime
  • Posts: 34
  • Karma: 0
Dear Tony:

How are you?

We are trying to use this java code in limesurvey 2.00+ build 130526, where the q1 is an array number checkbox and q2 is a text array.

But it doesn't work.

Is it possible that this script doesn't work with this type of question?

Thanks and regards Ariel
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 4.938 seconds