Check out the LimeSurvey source code on GitHub!
Welcome, Guest
Username: Password:

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

Multiple types in array - rank all then choose some items 2 years 3 weeks ago #110594

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6114
  • Thank you received: 1356
  • Karma: 532
Hi Elisa,

Try this:

<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)
		$('#movenextbtn, #movesubmitbtn').bind('click', function () {
 
			$('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>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.
The following user(s) said Thank You: elisa

Multiple types in array - rank all then choose some items 2 years 2 weeks ago #110690

  • elisa
  • elisa's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 66
  • Thank you received: 23
  • Karma: 8
Thanks a lot Tony. It works perfecty.
Cheers,
Elisa
Elzbieta Lesinska
LS Polish translator and supervisor
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.199 seconds
Imprint                   Data Protection Statement                  Revocation information and revocation form