Welcome, Guest
Username: Password: Remember me

TOPIC: Multiple types in array - rank all then choose some items

Multiple types in array - rank all then choose some items 3 months 3 weeks ago #102971

  • elisa
  • elisa's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 8
  • Thank you received: 2
  • Karma: 0
Hi,
I've searched the formum and manual and cannot find the solution. I need to ask an array question consisting of several items that all need to be assessed. After assessing all the respondent should choose some, let's say 5, most important items. I would like to have it in one table as in the picture (colors are not important). I was thinking about Multiple question types in array workaround but I'm not sure if I can apply it here. Any advice? Thanks.
Elisa

array.jpg
The administrator has disabled public write access.

Multiple types in array - rank all then choose some items 3 months 3 weeks ago #102975

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3844
  • Thank you received: 690
  • Karma: 330
I would use a solution similar to this post where we moved input elements from a following question into the array.

In your case the following question would be a multiple-choice type and the input element selector would be "input.text".

Would it be mandatory that 5 check-boxes ar selected?
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.

Multiple types in array - rank all then choose some items 3 months 3 weeks ago #102976

  • elisa
  • elisa's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 8
  • Thank you received: 2
  • Karma: 0
Thanks Tony, I'll try it. Yes, the 5 check-boxes are mandatory. Or, alternatively, up to 5 check-boxes are mandatory.
The administrator has disabled public write access.

Multiple types in array - rank all then choose some items 3 months 3 weeks ago #102977

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3844
  • Thank you received: 690
  • Karma: 330
Hi Elisa, here's a quick adaptation of that post.

This script moves the check-boxes from a following question into the array and validates for 5 checked boxes.

1) Add this to the question source and modify the error messages as required:
<script type="text/javascript" charset="utf-8">	
 
	$(document).ready(function(){
 
		// The error messages
		var error1 = 'You can only select 5 attributes.';
		var error2 = 'Please select 5 attributes.';
 
		// Identify the questions
		var thisQuestion = $('#question{QID}');
		var nextMultiOpt = $(thisQuestion).nextAll('.multiple-opt:eq(0)');
 
		// Add a class to the array question (for styling) 
		$(thisQuestion).addClass('array-plus-checkboxes');
 
		// Hide the next question 
		$(nextMultiOpt).hide();
 
		// Add an extra column to the array
		$('table.question thead tr', thisQuestion).append('<th class="newColumn1" />');
		$('table.question tbody tr', thisQuestion).each(function(i) {
			$(this).append('<td class="newColumn1" />');
		});
 
		// Move the hidden question text to the inserted column
		$('table.question thead tr th.newColumn1', thisQuestion).text($('.questiontext', nextMultiOpt).text());
 
		// Move the checkboxes into the array
		$('input.checkbox', nextMultiOpt).each(function(i){
			$('table.question tbody tr:eq('+i+') td.newColumn1', thisQuestion).append(this);
		});
 
		// Listener on the checkboxes (to test for maximum 5 checked boxes)
		$('input.checkbox', thisQuestion).click(function(event) {
			if($('input.checkbox:checked', thisQuestion).length > 5) {
				alert(error1);
				$(this).attr('checked', false);
			}
			if($('input.checkbox:checked', thisQuestion).length == 5) {
				$('th.newColumn1', thisQuestion).removeClass('mandatory-error');
			}
		});
 
		// Interrupt the Next/Submit function (to test for minimum 5 checked boxes)
		$('form#limesurvey').submit(function(){
 
			// Override the built-in "disable navigation buttons" feature
			$('#moveprevbtn, #movenextbtn, #movesubmitbtn').attr('disabled', '');
 
			$('th.newColumn1', thisQuestion).removeClass('mandatory-error');
 
			if($('input.checkbox:checked', thisQuestion).length < 5) {
				$('th.newColumn1', thisQuestion).addClass('mandatory-error');
				alert(error2);
				return false;
			}
		});
 
		// Some cleanup styles
		$('col', thisQuestion).css({
			'width': 'auto'
		});
 
	});
</script>

2) Add something like this to the end of template.css:
.array-plus-checkboxes tbody td {
	width: 12%;
}
 
.array-plus-checkboxes td.newColumn1 {
	width: 15%;
}
 
.array-plus-checkboxes .newColumn1 {
	background-color: #FF6;
}
 
.array-plus-checkboxes .newColumn1.mandatory-error {
	color: #FF0000;
}

Here is a sample survey:

File Attachment:

File Name: test_move_checkboxes_into_array.lss
File Size: 27 KB
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.

Multiple types in array - rank all then choose some items 3 months 3 weeks ago #102978

  • elisa
  • elisa's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 8
  • Thank you received: 2
  • Karma: 0
Tony, you know you are great! It works. Thank you very much. Chapeau bas. I tried to adapt the script myself but I'm too JS illiterate to do that. I wish you all the best in the New Year!
The administrator has disabled public write access.

Multiple types in array - rank all then choose some items 3 months 3 weeks ago #102979

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3844
  • Thank you received: 690
  • Karma: 330
Happy New Year to you and your family too.
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: DenisChenu
Moderators: ITEd
Time to create page: 0.122 seconds
Donation Image