Adding textbox in basic array

More
5 months 5 hours ago #170068 by krosser
krosser replied the topic: Adding textbox in basic array
Hi guys,

I need to make an array question with textboxes similar to this



I have a code from Tony to place the textboxes in different columns.
<script type="text/javascript" charset="utf-8">
	$(document).on('ready pjax:scriptcomplete',function(){
		$('#question{QID}').cbArrayComments3({
			column: 3 // Text Input column (-1 will default to last column)
		});
	});
</script>

But when you type in the textboxes, the other radio buttons are automatically unchecked. Usually, it is what you would want, but in my case the comment is for one of the answer columns. So I wonder if it is possible to modify the code to allow the radio buttons be checked while keeping the text?

Many thanks! :)

I'm using the latest LS 3.15 hosted on LS servers, not installed locally.
Attachments:

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

More
4 months 4 weeks ago #170112 by krosser
krosser replied the topic: Adding textbox in basic array
Well, in my case, the change in cbArrayComments worked. :laugh:
<script type="text/javascript" charset="utf-8">
	$(document).on('ready pjax:scriptcomplete',function(){
		$('#question{QID}').cbArrayCommentsgov2q4a({
			column: 3 // Text Input column (-1 will default to last column)
		});
	});
</script>

I'm using the latest LS 3.15 hosted on LS servers, not installed locally.

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

More
4 months 4 weeks ago #170114 by krosser
krosser replied the topic: Adding textbox in basic array
Hey Tony, do you think it is possible to move two multiple short text questions on top an array? Or perhaps an array(texts) to an array? I have to make a multiple choice question with two textbox columns, similar to this:

I'm using the latest LS 3.15 hosted on LS servers, not installed locally.
Attachments:

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

More
4 months 4 weeks ago - 4 months 4 weeks ago #170234 by tpartner
tpartner replied the topic: Adding textbox in basic array
I would be inclined to use an array-texts type question and swap out some of the text inputs for checkbox inputs.

Add a script like this to the question source. The check-boxes will give a value of "Y" in the data when checked.

My time is limited this week so I will leave it to you to test the data and, perhaps, adjust styling.

<script type="text/javascript" charset="utf-8">
 
 
	$(document).on('ready pjax:scriptcomplete',function(){
 
		// Identify this question
		var thisQuestion = $('#question{QID}');
 
		// Column-specific classes
		$('tr.subquestion-list', thisQuestion).each(function(i) {
			$('th, td', this).each(function(i) {
				$(this).addClass('column-'+i);
			});
		});
 
		// Insert checkboxes
		$('.answer-item.column-1, .answer-item.column-2, .answer-item.column-3, .answer-item.column-4, .answer-item.column-7, .answer-item.column-8', thisQuestion).addClass('custom-checkbox-item');
		$('.custom-checkbox-item', thisQuestion).each(function(i) {
			var thisID = $('input:text:eq(0)', this).attr('id');
			$('label', this).before('<input class="" id="'+thisID+'" value="Y" type="checkbox" name="'+thisID.replace(/answer/, '')+'" />');
			if($('input:text:eq(0)', this).val() == 'Y') {
				$('input:checkbox:eq(0)', this).prop('checked', true);
			}
			$(this).removeClass('text-item').addClass('checkbox-item');
			$('input:text:eq(0)', this).remove();
		});
 
		// Identify exclusive items
		$('.answer-item.column-1, .answer-item.column-2, .answer-item.column-3, .answer-item.column-4, .answer-item.column-5, .answer-item.column-6', thisQuestion).addClass('non-exclusive-item');
		$('.answer-item.column-7, .answer-item.column-8', thisQuestion).addClass('exclusive-item');
 
		// Listeners for exclusive items
		$('.non-exclusive-item input:checkbox', thisQuestion).on('change', function(e) {
			if($(this).is(':checked')) {
				$(this).closest('tr.subquestion-list').find('.exclusive-item input:checkbox').prop('checked', false);
			}
		});
		$('.non-exclusive-item input:text', thisQuestion).on('keyup change', function(e) {
			if($.trim($(this).val()) != '') {
				$(this).closest('tr.subquestion-list').find('.exclusive-item input:checkbox').prop('checked', false);
			}
		});
		$('.exclusive-item input:checkbox', thisQuestion).on('change', function(e) {
			if($(this).is(':checked')) {
				var thisItem = $(this).closest('.answer-item');
				$(this).closest('tr.subquestion-list').find('.answer-item').not(thisItem).find('input:checkbox').prop('checked', false);
				$(this).closest('tr.subquestion-list').find('input:text').val('');
			}
		});
    });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...6-21.lss
File Size:23 KB

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

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

More
4 months 4 weeks ago #170296 by krosser
krosser replied the topic: Adding textbox in basic array
Wow! That is a great idea. :woohoo:
I have tested the code and it works nicely. You help is very much appreciated!

I'm using the latest LS 3.15 hosted on LS servers, not installed locally.

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

More
1 month 4 weeks ago #174270 by krosser
krosser replied the topic: Adding textbox in basic array

tpartner wrote: I would be inclined to use an array-texts type question and swap out some of the text inputs for checkbox inputs.

Add a script like this to the question source. The check-boxes will give a value of "Y" in the data when checked.

My time is limited this week so I will leave it to you to test the data and, perhaps, adjust styling.

<script type="text/javascript" charset="utf-8">
 
 
	$(document).on('ready pjax:scriptcomplete',function(){
 
		// Identify this question
		var thisQuestion = $('#question{QID}');
 
		// Column-specific classes
		$('tr.subquestion-list', thisQuestion).each(function(i) {
			$('th, td', this).each(function(i) {
				$(this).addClass('column-'+i);
			});
		});
 
		// Insert checkboxes
		$('.answer-item.column-1, .answer-item.column-2, .answer-item.column-3, .answer-item.column-4, .answer-item.column-7, .answer-item.column-8', thisQuestion).addClass('custom-checkbox-item');
		$('.custom-checkbox-item', thisQuestion).each(function(i) {
			var thisID = $('input:text:eq(0)', this).attr('id');
			$('label', this).before('<input class="" id="'+thisID+'" value="Y" type="checkbox" name="'+thisID.replace(/answer/, '')+'" />');
			if($('input:text:eq(0)', this).val() == 'Y') {
				$('input:checkbox:eq(0)', this).prop('checked', true);
			}
			$(this).removeClass('text-item').addClass('checkbox-item');
			$('input:text:eq(0)', this).remove();
		});
 
		// Identify exclusive items
		$('.answer-item.column-1, .answer-item.column-2, .answer-item.column-3, .answer-item.column-4, .answer-item.column-5, .answer-item.column-6', thisQuestion).addClass('non-exclusive-item');
		$('.answer-item.column-7, .answer-item.column-8', thisQuestion).addClass('exclusive-item');
 
		// Listeners for exclusive items
		$('.non-exclusive-item input:checkbox', thisQuestion).on('change', function(e) {
			if($(this).is(':checked')) {
				$(this).closest('tr.subquestion-list').find('.exclusive-item input:checkbox').prop('checked', false);
			}
		});
		$('.non-exclusive-item input:text', thisQuestion).on('keyup change', function(e) {
			if($.trim($(this).val()) != '') {
				$(this).closest('tr.subquestion-list').find('.exclusive-item input:checkbox').prop('checked', false);
			}
		});
		$('.exclusive-item input:checkbox', thisQuestion).on('change', function(e) {
			if($(this).is(':checked')) {
				var thisItem = $(this).closest('.answer-item');
				$(this).closest('tr.subquestion-list').find('.answer-item').not(thisItem).find('input:checkbox').prop('checked', false);
				$(this).closest('tr.subquestion-list').find('input:text').val('');
			}
		});
    });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...6-21.lss
File Size:23 KB



Hi Tony,

Can you please help out with modifying this code so that only certain text inputs are swapped out for checkbox inputs?

I need to make this type of question:



See, the 1st column is only 3 first rows and the last one is kept as a text input for the "other".

The relevant part of your original code:
<script type="text/javascript" charset="utf-8">
 
 
	$(document).on('ready pjax:scriptcomplete',function(){
 
		// Identify this question
		var thisQuestion = $('#question{QID}');
 
		// Column-specific classes
		$('tr.subquestion-list', thisQuestion).each(function(i) {
			$('th, td', this).each(function(i) {
				$(this).addClass('column-'+i);
			});
		});
 
		// Insert checkboxes
		$('.answer-item.column-1', thisQuestion).addClass('custom-checkbox-item');
		$('.custom-checkbox-item', thisQuestion).each(function(i) {
			var thisID = $('input:text:eq(0)', this).attr('id');
			$('label', this).before('<input class="" id="'+thisID+'" value="Y" type="checkbox" name="'+thisID.replace(/answer/, '')+'" />');
			if($('input:text:eq(0)', this).val() == 'Y') {
				$('input:checkbox:eq(0)', this).prop('checked', true);
			}
			$(this).removeClass('text-item').addClass('checkbox-item');
			$('input:text:eq(0)', this).remove();
		});
 
 
    });
</script>

File Attachment:

File Name: limesurvey...6125.lss
File Size:20 KB

I'm using the latest LS 3.15 hosted on LS servers, not installed locally.
Attachments:

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

More
1 month 4 weeks ago #174317 by tpartner
tpartner replied the topic: Adding textbox in basic array
Try this:

		// Insert checkboxes
		$('.answer-item.column-1:lt(3)', thisQuestion).addClass('custom-checkbox-item');
		...

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: krosser

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

More
1 month 4 weeks ago #174332 by krosser
krosser replied the topic: Adding textbox in basic array
Perfect! Thank you!
I wonder if that extra "lt" you added stands for "less than" or something else?

I'm using the latest LS 3.15 hosted on LS servers, not installed locally.

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

More
1 month 4 weeks ago - 1 month 4 weeks ago #174335 by tpartner
tpartner replied the topic: Adding textbox in basic array
Yes, :lt(3) targets elements with an index of less than 3. Since element indexing starts at 0, that targets all elements found before the fourth one.

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 1 month 4 weeks ago by tpartner.
The following user(s) said Thank You: holch, krosser

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

More
1 month 4 weeks ago #174344 by holch
holch replied the topic: Adding textbox in basic array
Works like a charm! Saved for when I might need it one day. ;-)

I'm not a LimeSurvey GmbH member. I answer at the LimeSurvey forum in my spare time. No support via private message.
Some helpful links: Manual (EN) | Question Types | Workarounds

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!