Welcome, Guest
Username: Password: Remember me

TOPIC: Filter answers of a multiple numeric type question

Filter answers of a multiple numeric type question 3 years 2 months ago #61089

  • madopr
  • madopr's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 26
  • Karma: 0
Hello: I'm trying to filter answers of a multiple numeric type question whith the answers checked in a multiple-option.
I tried inserting this code, found here in the multiple numeric question:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function(){
 
    // Match elements with labels using htmlFor and a loop
    var labels = document.getElementsByTagName('LABEL');
    for (var i = 0; i < labels.length; i++) {
        if (labels[i].htmlFor != '') {
             var elem = document.getElementById(labels[i].htmlFor);
             if (elem)
                elem.label = labels[i];
        }
    }
 
    var answerFilter="{INSERTANS:<multiple-option SGQ>}";
    for (i=0; i<30; i++){
      var iLabel = document.getElementById('answer<multiple numeric SGQ>'+(i+1)).label.innerHTML;
      if (answerFilter.search(iLabel) == -1){
        // Hide element
        $('#question<multiple numeric id>').find('li').eq(i).hide();
        // Set value of hidden elements to zero
        document.getElementById('answer<multiple numeric SGQ>'+(i+1)).value = 0;
      }
    }
  });</script>

but it doesn't work. What am I doing wrong?

[edit:] By the way, I don't care about the labels of the options; just the ids (in fact, one of the labels changes), so maybe there's an easier way to do this using just the ids of the subquestions
Last Edit: 3 years 2 months ago by madopr.
The administrator has disabled public write access.

Re: Filter answers of a multiple numeric type question 3 years 2 months ago #61102

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4049
  • Thank you received: 739
  • Karma: 341
I would use a hidden multiple options question and array filter to pass the checked option codes into page 2.

1) Insert a copy of the multiple options question (q1) at the top of group 2. Let's call it "qHidden" (we'll hide it with JavaScript). All answer codes in q1, qHidden and the multi-numeric must be identical.

2) Apply an array filter to qHidden using the q1 ID.

3) Insert the following script into the source of qHidden. Replace "HH" with the ID of qHidden and "NN" with the ID of the multi-numeric question.

The script looks for all hidden (by array filter) options in qHidden, hides the corresponding options in the multi-numeric and then hides the whole qHidden question.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		handleChecked3(HH, NN);
 
		function handleChecked3(q1ID, q2ID) {
 
			// Find the survey and group IDs
			if($( 'input#fieldnames' ).length != 0) {
				var fieldNames = $( 'input#fieldnames' ).attr('value');
				var tmp = fieldNames.split('X');
				var sID = tmp[0];
				var gID = tmp[1];
			}
 
			// An empty array for the answer codes
			var optCodes = new Array();
 
			// Loop through all hidden (by array filter) options in qHidden
			$('#question'+q1ID+' li[id^="javatbd"]:hidden').each(function(i) {
 
				// Find the option code
				var itemID = $(this).attr('id');
				var tmp2 = itemID.split('X'+q1ID);
				var optCode = tmp2[1];
 
				// Add the code to the array
				optCodes.push(optCode);
 
			});
 
			// Loop through the array of hidden option codes
			$(optCodes).each(function(i) {
 
				// Find the corresponding list item in the multi-numeric
				var el = $('#question'+q2ID+' label[for="answer'+sID+'X'+gID+'X'+q2ID+''+this+'"]').parent('li:eq(0)'); 
 
				// Hide the list item
				$(el).hide();
 
				// Set the list item input to 0
				$('input.text', el).val(0);
 
			});
 
			// Now, hide the whole qHidden question
			$('#question'+q1ID+'').hide();
		}
	});
 
</script>

Note, if you are using any of the "sum value" settings for the multi-numeric, you will need some extra code to calculate those.
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 answers of a multiple numeric type question 3 years 2 months ago #61133

  • madopr
  • madopr's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 26
  • Karma: 0
Can't I just do something like this??
for (var i = 1; i <= <number of labels>; i++){
	if (<subquestion i of multiple-option question is not checked>){
		<hide subquestion i of multiple-numeric question>;
	}
}

Oh, and yes, I need to use the sum value in the multiple numeric
Last Edit: 3 years 2 months ago by madopr.
The administrator has disabled public write access.

Re: Filter answers of a multiple numeric type question 3 years 2 months ago #61148

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4049
  • Thank you received: 739
  • Karma: 341
Can't I just do something like this??
Yeah, maybe I over-thought it. The second loop isn't required.

Line 39 should take care of the sum amount but you will need to replace "SSSSS", "GG" and "QQ" with the survey ID, group ID and multi-numeric question ID respectively.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		handleChecked3(HH, NN);
 
		function handleChecked3(q1ID, q2ID) {
 
			// Find the survey and group IDs
			if($( 'input#fieldnames' ).length != 0) {
				var fieldNames = $( 'input#fieldnames' ).attr('value');
				var tmp = fieldNames.split('X');
				var sID = tmp[0];
				var gID = tmp[1];
			}
 
			// Loop through all hidden (by array filter) options in qHidden
			$('#question'+q1ID+' li[id^="javatbd"]:hidden').each(function(i) {
 
				// Find the option code
				var itemID = $(this).attr('id');
				var tmp2 = itemID.split('X'+q1ID);
				var optCode = tmp2[1];
 
				var el = $('#question'+q2ID+' label[for="answer'+sID+'X'+gID+'X'+q2ID+''+optCode+'"]').parent('li:eq(0)'); 
 
				// Hide the corresponding list item in Q2
				$(el).hide();
 
				// Set the corresponding input in Q2 to 0
				$('input.text', el).val(0);
 
			});
 
			// Hide Q1
			$('#question'+q1ID+'').hide();
 
			// Calculate for sum amount
			calculateValueSSSSSXGGXQQ(3) // NOTE: this needs to use local survey, group and multi-nemeric question IDs
		}
	});
 
</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.171 seconds
Donation Image