Array numbers, yes checkbox in the last column

More
3 years 7 months ago #118840 by granen
granen created the topic: Array numbers, yes checkbox in the last column
Hi,

Is it possible to use checkboxes only in the last column of an array number question type? I need ten columns first where you add numbers and a last one that is a "I don't know" checkbox. The use checkbox option but only for one column.

//Erik

Please Log in or Create an account to join the conversation.

More
3 years 7 months ago #118869 by tpartner
tpartner replied the topic: Array numbers, yes checkbox in the last column
Are you using text inputs or drop-downs?

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in or Create an account to join the conversation.

More
3 years 7 months ago #118870 by granen
granen replied the topic: Array numbers, yes checkbox in the last column
Text inputs!

Please Log in or Create an account to join the conversation.

More
3 years 7 months ago - 3 years 7 months ago #118871 by tpartner
tpartner replied the topic: Array numbers, yes checkbox in the last column
Okay, then place the following script in the source of the array.

It will:
  • Hide the text inputs in the last column
  • Insert checkboxes in those cells
  • When a checkbox is checked, all visible text inputs in that row are cleared and the hidden input is given a value of 1
  • When a value is entered in a visible text input, the checkbox in that row is unchecked and the hidden input is cleared

<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
 
		// Identify this question
		var thisQuestion = $('#question{QID}')
 
		// Add some classes to the answer cells
		$('td.question-item', thisQuestion).addClass('normal-opt');
		$('tr.subquestions-list', thisQuestion).each(function(i) {
				$('.normal-opt:last', this).removeClass('normal-opt').addClass('exlusive-opt')
		});
 
		// Insert the checkboxes
		$('.exlusive-opt input[type="text"]', thisQuestion).hide();
		$('.exlusive-opt', thisQuestion).append('<input type="checkbox" class="checkbox">');
 
		// A listener on the text inputs
		$('.normal-opt input[type="text"]', thisQuestion).on('keyup change paste', function() {
			var thisInput = $(this);
			var thisRow = $(this).closest('tr.subquestions-list');
			setTimeout(function() {
				if(thisInput.val() != ''){
					$('.exlusive-opt input[type=checkbox]', thisRow).prop('checked', false);
					$('.exlusive-opt input[type=text]', thisRow).val('');
				}
			}, 100);
		});	
 
		// A listener on the checkboxes
		$('.exlusive-opt input[type="checkbox"]', thisQuestion).change(function(event) {
			var thisCell = $(this).closest('td.question-item');
			var thisRow = $(this).closest('tr.subquestions-list');
			if($(this).is(':checked')) {
				$('input[type=text]', thisCell).val(1);
				$('.normal-opt input[type=text]', thisRow).val('');
			}
			else {
				$('input[type=text]', thisCell).val('');
			}
		});
 
		// Initial states
		$('.exlusive-opt input[type="text"]', thisQuestion).each(function(i) {
			var thisCell = $(this).closest('td.question-item');
			if($(this).val() != '') {
				$('input[type=checkbox]', thisCell).prop('checked', true);				
			}
		});
	});
</script>

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 3 years 7 months ago by tpartner.
The following user(s) said Thank You: granen

Please Log in or Create an account to join the conversation.

More
3 years 7 months ago #118874 by granen
granen replied the topic: Array numbers, yes checkbox in the last column
It works like a charm!

A big thank you for that.

Please Log in or Create an account to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!