Array question with answers in dropdown list

More
3 weeks 4 days ago #169270 by Someone10
Someone10 created the topic: Array question with answers in dropdown list
Hi, I used this code for a survey

<script type="text/javascript" charset="utf-8">

$(document).ready(function(){

$('#question9769 .answer_cell_00SQ001 option[value=1]').text('No recurro');
$('#question9769 .answer_cell_00SQ001 option[value=2]').text('Arocena Gustavo');
$('#question9769 .answer_cell_00SQ001 option[value=3]').text('Aspra Manuel');
$('#question9769 .answer_cell_00SQ001 option[value=4]').text('Bravo Ricardo');
$('#question9769 .answer_cell_00SQ001 option[value=5]').text('Zalazar Martin');

$('#question9769 .answer_cell_00SQ002 option[value=1]').text('No recurro');
$('#question9769 .answer_cell_00SQ002 option[value=2]').text('Bravo Ricardo');
$('#question9769 .answer_cell_00SQ002 option[value=3]').text('Fernandez Flavio');
$('#question9769 .answer_cell_00SQ002 option[value=4]').text('Zalazar Martin');
$('#question9769 .answer_cell_00SQ002 option[value=5]').text('').hide();

});

</script>

But dont work, i used the Versión 3.8.0+180522.

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

More
3 weeks 4 days ago #169278 by tpartner
tpartner replied the topic: Array question with answers in dropdown list
Can you provide an export of a small sample survey?

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 weeks 3 days ago #169294 by Someone10
Someone10 replied the topic: Array question with answers in dropdown list
Ok
Attachments:

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

More
3 weeks 3 days ago #169319 by tpartner
tpartner replied the topic: Array question with answers in dropdown list
Try this:

<script type="text/javascript" charset="utf-8">
 
	$(document).on('ready pjax:scriptcomplete',function(){
 
		$('#question{QID} .answer_cell_SQ001 option[value=1]').text('No recurro');
		$('#question{QID} .answer_cell_SQ001 option[value=2]').text('Arocena Gustavo');
		$('#question{QID} .answer_cell_SQ001 option[value=3]').text('Aspra Manuel');
		$('#question{QID} .answer_cell_SQ001 option[value=4]').text('Bravo Ricardo');
		$('#question{QID} .answer_cell_SQ001 option[value=5]').text('Zalazar Martin');
 
		$('#question{QID} .answer_cell_SQ002 option[value=1]').text('No recurro');
		$('#question{QID} .answer_cell_SQ002 option[value=2]').text('Bravo Ricardo');
		$('#question{QID} .answer_cell_SQ002 option[value=3]').text('Fernandez Flavio');
		$('#question{QID} .answer_cell_SQ002 option[value=4]').text('Zalazar Martin');
		$('#question{QID} .answer_cell_SQ002 option[value=5]').text('').hide();
 
	});
 
</script>

Here is your sample survey with that change:

File Attachment:

File Name: limesurvey...9661.lss
File Size:18 KB

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

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

More
3 weeks 3 days ago #169321 by Someone10
Someone10 replied the topic: Array question with answers in dropdown list
Thank you very much tony, it works. But another question arises. Is there any way to put in the middle of those columns a column in which you can enter text?

If there was some kind of code that would allow me in one of those columns to add text, I would fix many problems

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

More
3 weeks 3 days ago #169325 by tpartner
tpartner replied the topic: Array question with answers in dropdown list
For that, you could extend this workaround for separate columns - www.limesurvey.org/forum/can-i-do-this-w...al-text-value#168857

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 weeks 3 days ago #169330 by Someone10
Someone10 replied the topic: Array question with answers in dropdown list
I have imported the survey from that page with the code and I get the table without a column with text to add, what could have gone wrong?

Attachments:

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

More
3 weeks 3 days ago #169344 by tpartner
tpartner replied the topic: Array question with answers in dropdown list
Yes, that's why I said you would have to extend the workaround for different columns.

<script type="text/javascript" charset="utf-8">
	$(document).on('ready pjax:scriptcomplete',function(){
		var thisQuestion = $('#question{QID}');
 
		// Insert selects
		$('.answer-item.answer_cell_X001', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
													<option value="">Please choose...</option>\
													<option value="1">Yes</option>\
													<option value="2">No</option>\
													<option value="3">Do not know</option>\
												</select>'); 
		$('.answer-item.answer_cell_X002', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
													<option value="">Please choose...</option>\
													<option value="1">Red</option>\
													<option value="2">Blue</option>\
													<option value="3">Pink</option>\
													<option value="3">Purple</option>\
												</select>');  
 
		// Listeners
		$('.inserted-select', thisQuestion).on('change', function(i) {
			if($(this).val() != '') {
				$(this).closest('.answer-item').find('input:text').val($.trim($('option:selected', this).text())).trigger('change');
			}
			else {
				$(this).closest('.answer-item').find('input:text').val('').trigger('change');
			}
		});
 
		// Returning to page
		$('.with-select input:text', thisQuestion).each(function(i) {
			var thisCell = $(this).closest('.answer-item');
			var inputText = $.trim($(this).val());
			var selectval = $('select.inserted-select option', thisCell).filter(function () { return $(this).html() == inputText; }).val();
			$('select.inserted-select', thisCell).val(selectval);
		});
 
		// Clean-up styles
		$('select.inserted-select', thisQuestion).css({
			'max-width': '100%'
		});
		$('.with-select input:text', thisQuestion).css({
			'position': 'absolute',
			'left': '-9999em'
		});
	});
</script>



Survey attached:

File Attachment:

File Name: limesurvey...1969.lss
File Size:21 KB

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

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

More
3 weeks 2 days ago - 3 weeks 2 days ago #169365 by Someone10
Someone10 replied the topic: Array question with answers in dropdown list
thank you very much, one last question, if I want to add more select, how could I do? I want to do as in the image that I attached, that X004, X005 and X007 are dropdown
Attachments:
Last Edit: 3 weeks 2 days ago by Someone10.

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

More
3 weeks 2 days ago #169371 by Joffm
Joffm replied the topic: Array question with answers in dropdown list
Hi,
I am sure you can find out by yourself.

In the javascript code you see
// Insert selects
		$('.answer-item.answer_cell_X001', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
													<option value="">Please choose...</option>\
													<option value="1">Yes</option>\
													<option value="2">No</option>\
													<option value="3">Do not know</option>\
												</select>');
You see the X001 refers to the column code.

So just define your rows and columns and add the appropriate lines in the script as I did here:
$('.answer-item.answer_cell_X005', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
													<option value="">Please choose...</option>\
													<option value="1">AAA</option>\
													<option value="2">BBB</option>\
													<option value="3">Do not know</option>\
												</select>'); 
		$('.answer-item.answer_cell_X006', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
													<option value="">Please choose...</option>\
													<option value="1">AAA</option>\
													<option value="2">BBB</option>\
													<option value="3">Do not know</option>\
												</select>'); 




Regards
Joffm
Attachments:

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

More
3 weeks 2 days ago #169377 by Someone10
Someone10 replied the topic: Array question with answers in dropdown list
I tried with this code but dont work
<script type="text/javascript" charset="utf-8">
	$(document).on('ready pjax:scriptcomplete',function(){
		var thisQuestion = $('#question{QID}');
 
		// Insert selects
		$('.answer-item.answer_cell_X004', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
													<option value="">Elige una opción...</option>\
													<option value="1">a</option>\
													<option value="2">b</option>\
												</select>'); 
		$('.answer-item.answer_cell_X005', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
													<option value="">Please choose...</option>\
													<option value="1">a</option>\
													<option value="2">b</option>\
													<option value="3">c</option>\
 
												</select>');
      $('.answer-item.answer_cell_X007', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
													<option value="">Please choose...</option>\
													<option value="1">a</option>\
													<option value="2">b</option>\
													<option value="3">c</option>\
 
												</select>');  
 
		// Listeners
		$('.inserted-select', thisQuestion).on('change', function(i) {
			if($(this).val() != '') {
				$(this).closest('.answer-item').find('input:text').val($.trim($('option:selected', this).text())).trigger('change');
			}
			else {
				$(this).closest('.answer-item').find('input:text').val('').trigger('change');
			}
		});
 
		// Returning to page
		$('.with-select input:text', thisQuestion).each(function(i) {
			var thisCell = $(this).closest('.answer-item');
			var inputText = $.trim($(this).val());
			var selectval = $('select.inserted-select option', thisCell).filter(function () { return $(this).html() == inputText; }).val();
			$('select.inserted-select', thisCell).val(selectval);
		});
 
		// Clean-up styles
		$('select.inserted-select', thisQuestion).css({
			'max-width': '100%'
		});
		$('.with-select input:text', thisQuestion).css({
			'position': 'absolute',
			'left': '-9999em'
		});
	});
</script>

with this survey
Attachments:

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

More
3 weeks 2 days ago #169382 by tpartner
tpartner replied the topic: Array question with answers in dropdown list
That is because you have introduced some white-spaces after the back-slashes - these are not allowed.

<script type="text/javascript" charset="utf-8">
	$(document).on('ready pjax:scriptcomplete',function(){
		var thisQuestion = $('#question{QID}');
 
		// Insert selects
		$('.answer-item.answer_cell_X004', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
													<option value="">Elige una opción...</option>\
													<option value="1">Certificada</option>\
													<option value="2">No Certificada</option>\
												</select>');
		$('.answer-item.answer_cell_X005', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
													<option value="">Please choose...</option>\
													<option value="1">Suya</option>\
													<option value="2">Cooperativa</option>\
													<option value="3">Otro</option>\
													<option value="3">Purple</option>\
												</select>');
		$('.answer-item.answer_cell_X007', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
													<option value="">Please choose...</option>\
													<option value="1">No hace nada</option>\
													<option value="2">Limpia Coop</option>\
													<option value="3">Limpia+Trata</option>\
												</select>');
 
		// Listeners
		$('.inserted-select', thisQuestion).on('change', function(i) {
			if($(this).val() != '') {
				$(this).closest('.answer-item').find('input:text').val($.trim($('option:selected', this).text())).trigger('change');
			}
			else {
				$(this).closest('.answer-item').find('input:text').val('').trigger('change');
			}
		});
 
		// Returning to page
		$('.with-select input:text', thisQuestion).each(function(i) {
			var thisCell = $(this).closest('.answer-item');
			var inputText = $.trim($(this).val());
			var selectval = $('select.inserted-select option', thisCell).filter(function () { return $(this).html() == inputText; }).val();
			$('select.inserted-select', thisCell).val(selectval);
		});
 
		// Clean-up styles
		$('select.inserted-select', thisQuestion).css({
			'max-width': '100%'
		});
		$('.with-select input:text', thisQuestion).css({
			'position': 'absolute',
			'left': '-9999em'
		});
	});
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...1925.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:

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

More
3 weeks 2 days ago #169389 by Someone10
Someone10 replied the topic: Array question with answers in dropdown list
Thank you very much for all, it works

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!